What is downcasting?

Consider the following example:
class B { … };
class D : public B { … };
void f()
{
B* pb = new D; // unclear but ok
B* pb2 = new B;
D* pd = dynamic_cast(pb); // ok: pb actually points to a D

D* pd2 = dynamic_cast(pb2); //error: pb2 points to a B, not a D
// pd2 == NULL

}
This type of conversion is called a “downcast” because it moves a pointer down a class hierarchy, from a given class to a class derived from it.

Subscribe / Share

It's very calm over here, why not leave a comment?

Leave a Reply




Categories

Powered by Yahoo! Answers