I have the following code.
#include <iostream>
using namespace std;
class Base
{
public:
virtual int f(){cout<<"Base"<<endl;}
};
class Derived:public Base
{
public:
int f(){cout<<"Derived"<<endl;}
};
int main()
{
Base b;
Derived d;
b.f(); ///base
((Base)d).f(); ///base
cout<<"----------------"<<endl;
Base *b1 = new Base;
Base *b2 = new Derived;
Derived *d1 = new Derived;
b1->f(); ///base
((Base*)d1)->f(); ///derived
((Base*)b2)->f(); ///derived
static_cast<Base*>(d1);
d1->f();///derived
static_cast<Base*>(b2);
b2->f();///derived
cout<<"----------------"<<endl;
Base *b5 = dynamic_cast<Base*>(b2);
Base *b6 = dynamic_cast<Base*>(d1);
if(b5)
b5->f(); ///derived
if(b6)
b6->f(); ///derived
return 0;
}
I want to ask why the derived *d1 OR b2 pointers when converted to a base using explicit cast (Base), static cast (static_cast(d1)) or dynamic cast (dynamic_cast(d1)) won't call the f() function of the base class after the conversion. It seems to call the f() function from the derived class every single time.
Also, strangely when I declare the objects this way. The conversion works and calls the base function.
Base b;
Derived d;
b.f(); ///base
((Base)d).f(); ///base
Now, I understood that the right way to access the f() from Base class would be d->Base::f()
, but why should I use dynamic_cast or static_cast since they won't convert the derived pointer to base and call the right function. I would need a detailed explanation if possible. Thanks!
Fortunately, for both you and my keyboard, the explanation is trivial:
((Base)d)
slices the objectd
to a value-copiedBase
instance.((Base*)d1)->f()
will still call the derived method sinceBase
and thereforeDerived
are polymorphic types, and although((Base*)d1)
is aBase*
pointer, it is pointing to aDerived
object. Ditto forstatic_cast
anddynamic_cast
.Reference: https://en.wikipedia.org/wiki/Object_slicing