I need to use dynamic cast void*
void *target = (MYClass*)target;//I am storing initially(to implment delegate mechanism)
....
delegateClass *delegate = dynamic_cast<delegateClass*>(target);
It is giving error cannot convert void*, I cannot use below code... since it is a delegate mechanism
delegateClass *delegate = dynamic_cast<delegateClass*>(((MYClass*))target);
How to get the type of target and implement... If i use typeid() i can get the name of the class but how to use typeid in the above equation instead of (((MYClass*))target).
You cannot use dynamic cast unless the original type of the variable had a vtable (ie, had virtual functions). This is because dynamic_cast requires run-time type information, which is recorded in the vtable; if the vtable is missing, the compiler doesn't know what type the object is.
You should declare a base class with a virtual destructor, and use pointers to this base class rather than void *
.
If you must pass the object as a void *
then you should use
delegateClass *delegate = static_cast<delegateClass*>(((MYClass*))target);
as there is no class relationship between the void *target
and delegateClass
. Here you are saying that you know that target _is_a_ delegateClass.
However this idiom is usually used for passing code through standard C interfaces and back.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With