I am quite confused with the dynamic_cast
keyword in C++.
struct A {
virtual void f() { }
};
struct B : public A { };
struct C { };
void f () {
A a;
B b;
A* ap = &b;
B* b1 = dynamic_cast<B*> (&a); // NULL, because 'a' is not a 'B'
B* b2 = dynamic_cast<B*> (ap); // 'b'
C* c = dynamic_cast<C*> (ap); // NULL.
A& ar = dynamic_cast<A&> (*ap); // Ok.
B& br = dynamic_cast<B&> (*ap); // Ok.
C& cr = dynamic_cast<C&> (*ap); // std::bad_cast
}
the definition says:
The
dynamic_cast
keyword casts a datum from one pointer or reference
type to another, performing a runtime check to ensure the validity of the cast
Can we write an equivalent of dynamic_cast
of C++ in C so that I could better understand things?
Best Answer
Here's a rundown on
static_cast<>
anddynamic_cast<>
specifically as they pertain to pointers. This is just a 101-level rundown, it does not cover all the intricacies.static_cast< Type* >(ptr)
This takes the pointer in
ptr
and tries to safely cast it to a pointer of typeType*
. This cast is done at compile time. It will only perform the cast if the types are related. If the types are not related, you will get a compiler error. For example:dynamic_cast< Type* >(ptr)
This again tries to take the pointer in
ptr
and safely cast it to a pointer of typeType*
. But this cast is executed at runtime, not compile time. Because this is a run-time cast, it is useful especially when combined with polymorphic classes. In fact, in certain cases the classes must be polymorphic in order for the cast to be legal.Casts can go in one of two directions: from base to derived (B2D) or from derived to base (D2B). It's simple enough to see how D2B casts would work at runtime. Either
ptr
was derived fromType
or it wasn't. In the case of D2B dynamic_cast<>s, the rules are simple. You can try to cast anything to anything else, and ifptr
was in fact derived fromType
, you'll get aType*
pointer back fromdynamic_cast
. Otherwise, you'll get a NULL pointer.But B2D casts are a little more complicated. Consider the following code:
main()
can't tell what kind of objectCreateRandom()
will return, so the C-style castBar* bar = (Bar*)base;
is decidedly not type-safe. How could you fix this? One way would be to add a function like boolAreYouABar() const = 0;
to the base class and returntrue
fromBar
andfalse
fromFoo
. But there is another way: usedynamic_cast<>
:The casts execute at runtime, and work by querying the object (no need to worry about how for now), asking it if it the type we're looking for. If it is,
dynamic_cast<Type*>
returns a pointer; otherwise it returns NULL.In order for this base-to-derived casting to work using
dynamic_cast<>
, Base, Foo and Bar must be what the Standard calls polymorphic types. In order to be a polymorphic type, your class must have at least onevirtual
function. If your classes are not polymorphic types, the base-to-derived use ofdynamic_cast
will not compile. Example:Adding a virtual function to base, such as a virtual dtor, will make both Base and Der polymorphic types: