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?