I need to write code for the callback function (it will be called from ATL, but this is not very important):
HRESULT callback( void* myObjectVoid ) { if( myObjectVoid == 0 ) { return E_POINTER; } CMyClass* myObject = static_cast<CMyClass*>( myObjectVoid ); return myObject->CallMethod(); }
here void* guaranteed to be a pointer to CMyClass , so static_cast is legal. My concern is that the code should be as portable as possible (to later versions of Visual C ++). Therefore, to be a superparanoid, I tend to also check the CMyClass* pointer - I mean, what if it turns out to be null?
if( myObjectVoid == 0 ) { return E_POINTER; } CMyClass* myObject = static_cast<CMyClass*>( myObjectVoid ); if( myObject == 0 ) { return E_POINTER; }
Is the second check reasonable? Is it possible for static_cast turn a non-null pointer into a null pointer?
c ++ pointers visual-c ++ static-cast
sharptooth
source share