"Ook" <zoo...@gmail.com> wrote in message
news:1173475785.793169.297100@h3g2000cwc.googlegroups.com...
I have a function getStuff, and two choices of implementation:
const string *getStuff()
{
return &_stuff;
}
or
const string getStuff()
{
return _stuff;
}
where _stuffis just a string: string _stuff;
I can call the second one like this:
string zoot;
zoot = getStuff;
Why would I want to use the first one in the above examples, and how
would I call it? I can't just use zoot = getStuff because I get a
compiler error.
Normally, I return strings by value unless they are large. If I am to
return a pointer to a string, I would rather return a reference. So really
you have 3 choices then.
const string* getstuff1()
{
return &stuff_;
}
const string& getstuff2()
{
return stuff_;
}
string getstuff3()
{
return stuff_;
}
const string* zoot1 = getstuff1();
const string& zoot2 = getstuff2();
string zoot3 = getstuff3();
Weather it needs to be const or not depends on what you plan on doing with
it.
Incidently, do no use _ to prefix your variable names, there are many cases
where the names are reserved by the OS (_ and a capital, two __, etc..) I
find it much better to add the _ at the end of my class variables.
Hi, thanks both of your for the clairifcation. I use the _ to prefix
do it <shrug>. Are there any generally accepted naming conventions for