tags:

views:

143

answers:

4

I'm getting a pointer to a base class (which is actually a pointer to some derived class). Then I want to call a function on that derived class, but I don't know which one it is.

class Base
{

};

class DerivedOne : public Base
{
    public:
     void functionA()
     { int x = 0; }
};

class DerivedTwo : public Base
{
    public:
     void functionA()
     { int x = 0; }
};

int main()
{   
    Base* derivedTwoPtr = new DerivedTwo();

    reinterpret_cast<DerivedOne*>(derivedTwoPtr)->functionA(); 

    return 0;
}

This works as I want, but I have to say it looks rather dodgy. Is it defined behavior? If not, is there a legal way to dynamically resolve this?

+5  A: 

Hey, don't do that. That's what virtual methods are for.

class Base
{
public:
    virtual void functionA()=0;

};

class DerivedOne : public Base
{
    public:
        virtual void functionA()
        {       int x = 0;      }
};

class DerivedTwo : public Base
{
    public:
        virtual void functionA()
        {       int x = 0;      }
};

int main()
{   
    Base* derivedTwoPtr = new DerivedTwo();

    derivedTwoPtr->functionA(); 

    return 0;
}
vava
+1  A: 

Just use virtual functions. That's what they are intended for. Your base class should look like

class Base
{
    virtual void functionA() = 0;
};

where the = 0 bit is optional. Omit it if you want to give a default implementation in the Base. If present the virtual function is known as a pure virtual function and enforces each subclass of Base to implement the function.

Now if you call functionA through a Base pointer you will get the method appropriate to whichever subclass the pointer really points to.

Troubadour
+1  A: 

You basically answered your own question here:

Casting to one class and calling function from sibling class?

This works as I want, but I have to say it looks rather dodgy. Is it defined behavior? If not, is there a legal way to dynamically resolve this?

In short:

if (DerivedOne* one=dynamic_cast<DerivedOne*>(BasePtr))
  one->functionA();
else if (DerivedTwo* two=dynamic_cast<DerivedTwo*>(BasePtr))
  two->functionA();

But yeah, like vava said, don't do that.

1800 INFORMATION
the same deal, dynamic_cast doesn't work without presence of at least one virtual method in base class.
vava
+1  A: 

is there a legal way to dynamically resolve this?

dynamic_cast can be used to cast to a specific derived class and invoke derived class methods. But in your case the best would be to provide a virtual method in Base class and provide different implementation for the virtual method in derived classes.

aJ
dynamic_cast won't work properly unless there's virtual method in base class.
vava