views:

230

answers:

2

Okay, I'm pretty inexperienced as a programmer, let alone in C++, so bear with me here. What I wanted to do was to have a container class hold a parent class pointer and then use polymorphism to store a child class object. The thing is that I want to call one of the child class's functions through the parent class pointer. Here's a sort of example of what I mean in code:

class SuperClass
{
public:
    int x;
}

class SubClass : public SuperClass
{
public:
    void function1()
    {
        x += 1;
    }
}

class Container
{
public:
    SuperClass * alpha;
    Container(SuperClass& beta)
    {
        alpha = beta;
    }
}

int main()
{
    Container cont = new Container(new SubClass);
}

(I'm not sure that's right, I'm still really shaky on pointers. I hope it gets the point across, at least.)

So, I'm not entirely sure whether I can do this or not. I have a sneaking suspicion the answer is no, but I want to be sure. If someone has another way to accomplish this sort of thing, I'd be glad to hear it.

+4  A: 

Definitely possible. Just a few small changes to your code should get this to work. You need to change the base class (SuperClass) to have a virtual method with the same 'signature' as the derived class (SubClass). You also need to change the Container constructor to take a pointer to the SuperClass instead of a reference. I believe that is all that should be necessary to get this to work.

EDIT: Attempted to include the suggestions in the comments. Good points. Hopefully I didn't mess this up too badly!

class SuperClass
{
public:
    int x;

    // virtual destructor ensures that the destructors of derived classes are also
    // invoked
    virtual ~SuperClass() { }

    // abstract virtual method to be overriden by derived classes
    virtual void function1() = 0;
}

class SubClass : public SuperClass
{
public:
    ~SubClass()
    {
    }

    void function1()
    {
        x += 1;
    }
}

class Container
{
public:
    SuperClass * alpha;
    Container(SuperClass* beta)
    {
        alpha = beta;
    }

    ~Container()
    {
       // Note, this will invoke the destructor of the SubClass class because 
       // SuperClass's destructor is marked as virtual.
       delete alpha;
    }
}

int main()
{
    Container* cont = new Container(new SubClass());
    delete cont;
}
Andrew Garrison
I would like to point out that, if function1 is only to be defined by subclasses, you might want to make its declaration `virtual void function1()=0;` this makes SuperClass abstract and requires that it be implemented in a subclass, enforcing the polymorphism.
Matt
Also note that the example is leaking a SubClass and a Container.
Fozi
It's not really a leak if it lasts to the end of the program, but there should be a `Container` destructor like `~Container() { delete alpha; }`. Assuming of course that the `Container` is to own the `SuperClass` or `SubClass` it points to.
David Thornley
Oh, and the declaration in `main()` needs to be either `Container * cont = new Container(new SubClass);` or `Container cont(new SubClass);`. The type of `cont` has to be the same as the value it's initialized with.
David Thornley
And `SuperClass` needs a virtual destructor, unless you plan to fish it out of the container and cast it back to `SubClass` before deleting it.
Mike Seymour
A: 

You got to change the line :

Container(SuperClass& beta)

to

Container(SuperClass* beta)

Oops, i forgot virtual.

hype