2

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?

drby
  • 2,619
  • 25
  • 26
  • 1
    You already got the answers on how to do what you want; to answer the other part of your question, no, it's entirely undefined behavior. – Pavel Minaev Oct 05 '09 at 07:03

4 Answers4

7

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
  • 24,851
  • 11
  • 64
  • 79
3

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. 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
  • 13,334
  • 2
  • 38
  • 57
2

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.
  • 34,624
  • 22
  • 86
  • 128
1

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
  • 131,367
  • 29
  • 160
  • 239
  • the same deal, dynamic_cast doesn't work without presence of at least one virtual method in base class. – vava Oct 05 '09 at 10:43