I'm testing a new forum search which hopefully works better than the existing one. It currently only indexes public posts. Feedback welcome-- ngld
0 Members and 1 Guest are viewing this topic.
"Some programmers like to program defensively," wrote Sam, "and then there's some of my coworkers. This is found at the top of nearly every function of our C++ classes." if (!this) return false;
They are checking to see if the THIS pointer is null. The freaking THIS POINTER! It the this pointer is null YOU COULDNT BE CALLING THE FUNCTION IN THE FREAKING FIRST PLACE.
bool MyClass::ReturnTrue(){ return true;}MyClass* class = 0;class->ReturnTrue(); //will return true
bool MyClass::ReturnTrue() { return true;}//is converted by the compiler to:bool MyClass::ReturnTrue(MyClass*) { return true;}class-> ReturnTrue(); //is converted by the compiler to:ReturnTrue(class);//well, not exactly, but you get the idea
Therefore I should revise my statement to you SHOULDN'T be calling the function in the first place (I've had many bugs where the call stack would get corrupted because something returned an invalid pointer and I ended up calling a function on it).
Quote from: blackhole on May 16, 2009, 03:25:39 amTherefore I should revise my statement to you SHOULDN'T be calling the function in the first place (I've had many bugs where the call stack would get corrupted because something returned an invalid pointer and I ended up calling a function on it).I can only agree with you here
_AFXWIN_INLINE CGdiObject::operator HGDIOBJ() const { return this == NULL ? NULL : m_hObject; }
MyAwesomeClass* pMAC = CreateNewAwesomeClass( );pMSC->DoCoolFunction( );
MyAwesomeClass* pMAC;pMAC->DoCoolFunction( );
Thou shalt cast all function arguments to the expected type if they are not of that type already, even when thou art convinced that this is unnecessary, lest they take cruel vengeance upon thee when thou least expect it.