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

metaform3d's Avatar, Join Date: Mar 2012
Newbie Member
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?
0
metaform3d's Avatar, Join Date: Mar 2012
Newbie Member
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?
0
metaform3d's Avatar, Join Date: Mar 2012
Newbie Member
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?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Post your complete code; it's impossible to find bugs in code we can't see.
0
metaform3d's Avatar, Join Date: Mar 2012
Newbie Member
It wasn't a question about bugs in my code. It was a question about how to approach a programming problem.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
OK. What are your outstanding questions?