Jump to: navigation, search

Dead C++ Society/Material/casts

Dead C++ Society/Material/casts


dynamic_cast

Standard & complex case:

class A { virtual void foo() {}; int dummyA; };
class B : virtual public A { int dummy; };
class D : public B {};

class C : virtual public A { float dummy; };
class Monster : public D, public C {};

int main()
{
   B *b = new Monster;
   C *c = dynamic_cast<C*>(b);
   assert(c != 0);
}

Doesn't compile because A is not polymorphic:

class A {};
class B : public A {};
int main()
{
   A *a = new B;
   B *b = dynamic_cast<B*>(a);
}

C casts vs C++ casts

http://cboard.cprogramming.com/faq-board/86924-faq-difference-between-c-cplusplus-style-casting.html