PDA

View Full Version : Is this legal



Orphious
09-21-01, 11:18 AM
If I want to return a string, what type function would it be.

Would it be string like this?

string text() const
{
string answer;
if(blah==Yes)
answer="YES";
else is(blah==no)
answer = "NO";
else
answer = "POOP";
return answer;
}

It couldn't be a void cause you're returning something right? It works with string but is that right?

Thanks

Stu
09-22-01, 09:50 PM
That works. Although there are other ways to do it, like passing a reference into the function and returning nothing.

Also, there is no real need to declare the function as const, unless it's a method (a.k.a. member function) that does not modify its' class object's state. Because functions operate within their own scope and sometimes on their parameters. You can typically assume they will not modify anything outside of their scope, and may only modify their parameters if they are passed in as pointers or references.

Orphious
09-23-01, 08:41 AM
Ya, it is actually a member function of a class so thats why I made it a const. Thanks again Stu. I plan to bug you with many more questions. Lucky you huh?:rotfl: