Quote:Original post by Nypyren
If I had to implement dynamic_cast in a compiler, I'd just CMP the object's vtable pointer to the compiler-known vtable pointer for the type in question, return the object if they match or NULL otherwise.
Unfortunately that won't work in the following situation.
class A { public: virtual ~A(); };class B : public A {};class C : public B { virtual void next(); };B *Downcast(A* src){ return dynamic_cast<B*>(src);}void go(){ C *c = new C; B *b = Downcast(C);}
C will have a virtual table different than A and B. In the above example if(B::vtable == src->vtable) will return false and the dynamic cast will fail.