Monday, March 06, 2006

c++ Function Returns

Alas, after using Java compilers for so long now I made another ignorant assumption about c++. If you place your return value for your function inside any type of branching or looping structure make sure that at least one of the return statements can always be reached. If not you will have some major run-time errors that are hard to track down.
string SomeClass::getName() {
if(this->hasName())
return className;
}

In this example when the 'hasName()' function returns false nothing is returned.
But in the case below, even if 'hasName()' is false, something is returned from the function.
string SomeClass::getName() {
if(this->hasName())
return className;
else
return "";
}


Or as I've now learned to do. Turn On The Damn Compiler Warnings!

1 Comments:

Blogger Rosemary Welch said...

O God, that takes me way back to the 80's! Do you remember Structured COBOL? If...then...I wish I could go back to school, but alas. I am too busy! lol. Have a great day.

4/20/2006 11:51 PM  

Post a Comment

<< Home