Well, that is true, dynamic_cast does not work for void-pointers - once
you have converted your MyClass* pointer to a void* you have voluntarily
given up all type information and there is no safe way back.
To convert a void pointer back to MyClass*, you have to use
reinterpret_cast, which in this context is basically the same as the
C-style cast you used initially. There is, of course, no guarantee that
the object returned by reinterpret_cast is indeed a valid object of
MyClass or rather some other garbage.
If possible by any measure, avoid using void-pointers in C++ in the
first place - there is almost always a better way to do it. In C, where
no such thing like RTTI exists, there is one common idiom you could use
in C++ as well if you are urged to use void pointers: To provide at
least a certain degree of sanity checking before using a casted pointer,
include some signature/magic field in the struct/class and check that it
contains the expected value before using it:
void *p = ...
MyClass *my = reinterpret_cast<MyClass*>(p);
if ( my->Signature != MYCLASS_SIGNATURE )
{
// this pointer is invalid
}
else
{
// it is likely the the pointer is valid
}
If you can avoid using void-pointers, do that - it may save you a lot of
trouble.
--Johannes
Charles Zhang wrote:
Thank you very much. But I actually tried to use dynamic_cast, but I got
an compiler error
void* p = NULL;
...
...
MyClass* pClass = dynamic_cast<MyClass*>(p);
The compiler error is: "invalid expression type for dynamic_cast."
Then I changed the line to:
MyClass* pClass = dynamic_cast<MyClass*>((MyClass*)p);
Now, it compiles. But when p is not an valid pointer to MyClass, now run
time exception was thrown. Which could cause the application to crash.
Charles Zhang
Johannes Passing wrote:
>(MyClass*) is a C-style cast - in C++, reinterpret_cast, static_cast
and dynamic_cast are preferred. If you are not sure whether p really
points to a MyClass object or rather to an object of a different
class, use dynamic_cast:
http://msdn2.microsoft.com/en-us/lib...cs(VS.80).aspx
--Johannes
Charles Zhang wrote:
>>I want to know to the following in a safe manner (the following code
will be dangerous if p does not contain a pointer to MyClass). I
would like someone to show me a better way (either throw an exception
or return value which I can check if the cast is allowed or not.
void* p;
...
MyClass* pClass = (MyClass*)p;
Thank you very much for your help.
Charles Zhang
--
Johannes Passing -
http://int3.de/