I'm searching for the most common and robust way to check if a void* can be convert in a given C++ object type. You can see below some information about the context.
When I define a C API for a DLL, I often use void* to hide the C++ object I use behind (something like below)
typedef void* Instance_t;
int createInstance(Instance_t* pInst);
int processing(Instance_t inst, uint8_t* pBuf, size_t bufLength);
When I createInstance the code cast a pointer like that:
int createInstance(Instance_t* pInst)
{
MyClass* ptr = new MyClass();
*pInst = (void*)(ptr);
//.... etc
return 0;
}
But the question is how can we later in all the other C function we define, check if the void* value we receive is a valid MyClass*. I think we can't as none of the C++ casting operator is really type safe is that case (even dynamic_cast).
for now my best solution is to use a C cast (or a reinterpret_cast) and if everything is ok with a call to a IsValid function define with MyClass.
Have you a better way to do that check ?