dynamic_cast from "void *"

dimba picture dimba · Nov 9, 2010 · Viewed 33.4k times · Source

According to this, void* has no RTTI information, therefore casting from void* is not legal and it make sense.

If I remember correctly, dynamic_cast from void* was working on gcc.

Can you please clarify the issue.

Answer

vitaut picture vitaut · Nov 9, 2010

dynamic_cast works only on polymorphic types, i.e. classes containing virtual functions.

In gcc you can dynamic_cast to void* but not from:

struct S
{
    virtual ~S() {}
};

int main()
{
    S* p = new S();
    void* v = dynamic_cast<void*>(p);
    S* p1 = dynamic_cast<S*>(v); // gives an error
}