1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Accessing/allocating sub-classes from an abstract super-class

Discussion in 'C' started by metaform3d, Mar 6, 2012.

  1. metaform3d

    metaform3d New Member

    Joined:
    Mar 6, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    There must be a way to do this, but I must be some kind of idiot because any time I wade into this with C++ I get hopelessly confused. Any expert help appreciated!

    I want to create objects to represent a mathematical expression. This is a tree of expressions of different types, each one being an operator with sub-expressions. Simple enough, right? So I start with

    Code:
    class Expression {
        public:
        Expression        *exp_parent;
        Expression        *sub_exp[MAX_SUBEXP];
    
                 Expression (Expression * parent);
        virtual        ~Expression ();
    
        virtual int         OpTerm () = 0;
        virtual void         OpResult (float &, float *) = 0;
    
        virtual    Expression *     Copy (Expression *);
        void             Eval (float &);
    };
    
    The parent expression is passed as an argument to the constructor so we know it's always valid.

    Code:
    Expression::Expression (
        Expression        *parent)
    {
        exp_parent = parent;
    }
    
    Now we come to the first problem. In the destructor for the abstract class I want to read the number of subexpression (which varies with subclass) and destroy them as part of destroying the parent expression.

    Code:
    Expression::~Expression ()
    {
        int             i, n;
    
        n = OpTerm ();
        for (i = 0; i < n; i++)
            delete sub_exp[i];
    }
    
    No dice. The compiler complains:

    Code:
    exp.cpp(250): warning #705: call of pure virtual function
            n = OpTerm ();
                ^
    
    Is it really impossible to use methods in the subclass to implement the destructor for the common parts of the superclass?
     
  2. metaform3d

    metaform3d New Member

    Joined:
    Mar 6, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    But wait, there's more. Here's the real problem. I also want to create a Copy() method that will copy this expression and its subexpressions. Something like:

    Code:
    	Expression *
    Expression::Copy (
    	Expression		*parent)
    {
    	Expression		*copy;
    	int			 i, n;
    
    	copy = ???? (parent);
    
    	n = OpTerm ();
    	for (i = 0; i < n; i++)
    		copy->sub_exp[i] = sub_exp[i]->Copy (copy);
    
    	return copy;
    }
    
    The problem is what to put in the "???" part. How do I call the constructor for actual class implementing the abstract class. I conclude that I can't do it directly, but I'm hoping there's some technique for getting at it.

    My first attempt was to declare an Alloc(parent) method on the Expression class. Then in the subclasses I would do:

    Code:
    class ExpAdd : public Expression
    {
        public:
    			 ExpAdd (Expression *parent) : Expression (parent) {}
    	Expression *	 Alloc (Expression *parent)
    	{
    		return ExpAdd (parent);
    	}
    	...
    };
    
    The problem is I then have to do this same thing in every single subclass. There are dozens of operator types -- that's a lot of duplicated code. Is there a way to use a template to get rid of the noxious bit of boilerplate?
     
  3. metaform3d

    metaform3d New Member

    Joined:
    Mar 6, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    No one cares, I guess. OK, fine. Go4Expert fail.

    I did find a solution to most of these problems eventually, although the way I did it was to essentially re-create the pseudo-object-oriented techniques used in the original C code. Here's the deal C++: if I can't write better Object-Oriented code using you than using C, then why do you exist in the first place?
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Post your complete code; it's impossible to find bugs in code we can't see.
     
  5. metaform3d

    metaform3d New Member

    Joined:
    Mar 6, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    It wasn't a question about bugs in my code. It was a question about how to approach a programming problem.
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    OK. What are your outstanding questions?
     

Share This Page