dynamic_cast from “void *”
-
29-09-2019 - |
문제
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.
해결책
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
}
다른 팁
In 5.2.7 - Dynamic cast [expr.dynamic.cast]
it says that for dynamic_cast<T>(v)
:
- If
T
is a pointer type,v
shall be an rvalue of a pointer to complete class type - If
T
is a reference type,v
shall be an lvalue of a complete class type (thanks usta for commenting on my missing this)
...
- Otherwise,
v
shall be a pointer to or an lvalue of a polymorphic type
So, no, a (void*)
value is not allowed.
Let's think about what your request might mean: say you've got a pointer that's really to a Derived1*
, but the code dynamic_cast
-ing only knows it's a void*
. Let's say you're trying to cast it to a Derived2*
, where both derived classes have a common base. Superficially, you might think all the pointers would point to the same Base
object, which would contain a pointer to the relevant virtual dispatch table and RTTI, so everything could hang together. But, consider that derived classes may have multiple base classes, and therefore the needed Base
class sub-object might not be the one to which the Derived*
- available only as a void*
- is pointing. It wouldn't work. Conclusion: the compiler needs to know these types so it can perform some adjustment to the pointers based on the types involved.
Derived1* -----> [AnotherBase] [[VDT]Base] <-- but, need a pointer to start of [extra members] this sub-object for dynamic_cast
(Some answers talk about the need for the pointer you're casting from to be of a polymorphic type, having virtual functions. That's all valid, but a bit misleading. As you can see above, even if the void*
is to such a type it still wouldn't work reliably without the full type information, as the real problem is that void*
is presumably pointing to the start of the derived object, whereas you need a pointer to the base class sub-object from which the cast-to type derives.)
It is true that void*
can't be dynamically_cast
ed 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<A*>(p);
}
I get the following error:
cannot dynamic_cast 'p' (of type 'void*') to type 'struct A*' (source is not a pointer to class)
I guess you confuse with dynamic_cast
to void*
. That is legal and obtains the pointer to the most derived class object.
dynamic_cast
from void*
is illegal - the type casted from must be polymorphic - contain at least one virtual function (virtual destructor counts too).
You can cast a pointer to polymorphic type to void *
, but not vice versa.