i need to convert pointers to long (SendMessage()) and i want to safely check if the variable is correct on the otherside. So i was thinking of doing dynamic_cast but that wont work on classes that are not virtual. Then i thought of doing typeid but that will work until i pass a derived var as its base.
Is there any way to check if the pointer is what i am expecting during runtime? Is there a way i can use typeid to see if a pointer is a type derived from a particular base?
If all you have is a
long, then there’s not really much you can do. There is no general way to determine whether an arbitrary number represents a valid memory address. And even if you know it’s a valid memory address, there is no way to determine the type of the thing the pointer points to. If you can’t be sure of the real type of the thing before its address was cast tolong, then you can’t be sure that it’s going to be safe to cast thelongto whatever type you plan on casting it to.You’ll just have to trust that the sender of the message has sent you a valid value. The best you can do is to take some precautions to reduce the consequences to your own program when it receives a bogus value.