Suppose
what is the difference between:Code:class Derived: public Base{
...
};
ANDCode:Derived * d = new Derived;
Base* b = static_cast<Base*>(d);
Code:Derived d ;
Base b = static_cast<Base>(d);
Printable View
Suppose
what is the difference between:Code:class Derived: public Base{
...
};
ANDCode:Derived * d = new Derived;
Base* b = static_cast<Base*>(d);
Code:Derived d ;
Base b = static_cast<Base>(d);
Polymorphic relationships don't hold for actual objects.
That is:
A Derived* is a Base*
A Derived& is a Base&
But a Derived is NOT a Base. Such a conversion results in slicing, and you lose pieces of your objects.
The second one makes a copy of the object and casts it (which is where the slicing happens). The first one makes a copy of the pointer and casts it, so the object is still there.