Say I’ve got something like that in a public header:
class MyClass
{
public:
MyClass();
protected:
virtual ~MyClass();
private:
void *pSomeInternalClass;
}
And here’s my implementation:
MyClass::MyClass()
{
pSomeInteralClass = (void *) new SomeInternalClass();
}
MyClass::~MyClass()
{
SomeInternalClass *pTemp = (SomeInternalClass *) pSomeInternalClass;
delete pTemp;
pSomeInternalClass = NULL;
}
The idea behind the void pointer is that I dont’t have to include the definitions for the internal class in the public headers.
My question is: am I handling the void pointer in a safe way? Is there any problem with its deletion?
Thanks!
It’s safe as long as you cast it back to it’s original type.
But you don’t need to do that. Just forward-declare your internal type in your public header, and you don’t need to do this nasty cast.