Does dynamic_cast require virtual function?

c++

Solution

Yes it does, as per the standard, `dynamic_cast` can only downcast polymorphic types (i.e. a type with at least one virtual function)

Problem

For example: ``` class Animal { virtual void dummy() {}; //LINE1 } class Cat : public Animal { } Animal* a = new Cat(); if (Cat* c = dynamic_cast<Cat*> (a)) //LINE2 { //Do something. } ``` If I remove LINE1 from the Animal class (i.e. Animal class does not contain virtual members), LINE2 will not work.

Original source