dynamic_cast from “void *”

后端 未结 6 807
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 12:17

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

If I remember correctly,

6条回答
  •  抹茶落季
    2020-11-30 12:49

    It is true that void* can't be dynamically_casted from.

    You are probably mis-remembering. With g++ 4.5 and the following code

    struct A {
        virtual ~A();
    };
    
    int main() {
        A a;
        void *p = &a;
        A* pa = dynamic_cast(p);
    }
    

    I get the following error:

    cannot dynamic_cast 'p' (of type 'void*') to type 'struct A*' (source is not a pointer to class)

提交回复
热议问题