I have two classes, base class and a derived class. The base class has a virtual method.
Here is my test example:
class Base
{
public:
virtual void Hello() { cout << "-> Hello Base" << endl; }
};
class Derived: public Base
{
public:
void Hello() { cout << "-> Hello Derived" << endl; }
};
int main()
{
Base *mBase = new Base;
// something to do
....
Derived *mDerived = dynamic_cast<Derived*>(mBase);
mDerived->Hello();
return 0;
}
I'm looking to use the Hello()
method of the class derived after the cast of mBase
to mDerived
.
But the problem is that when I try to use dynamic_cast
it will crash the application, if not if I use reinterpret_cast
the Hello()
method of the Base
class will be called.
Result in the case dynamic_cast
:
Segmentation fault (core dumped)
Result in the case dynamic_cast
:
-> Hello Base