Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Accessing/allocating sub-classes from an abstract super-class (http://www.go4expert.com/forums/accessing-allocating-sub-classes-t27968/)

metaform3d 7Mar2012 04:00

Accessing/allocating sub-classes from an abstract super-class
 
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?

metaform3d 7Mar2012 04:09

Re: Accessing/allocating sub-classes from an abstract super-class
 
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?

metaform3d 13Mar2012 07:22

Re: Accessing/allocating sub-classes from an abstract super-class
 
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?

xpi0t0s 14Mar2012 04:30

Re: Accessing/allocating sub-classes from an abstract super-class
 
Post your complete code; it's impossible to find bugs in code we can't see.

metaform3d 14Mar2012 04:38

Re: Accessing/allocating sub-classes from an abstract super-class
 
It wasn't a question about bugs in my code. It was a question about how to approach a programming problem.

xpi0t0s 14Mar2012 13:28

Re: Accessing/allocating sub-classes from an abstract super-class
 
OK. What are your outstanding questions?


All times are GMT +5.5. The time now is 21:32.