const string *Item::getTitle() versus const string Item::getTitle()

O

Ook

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.
 
F

Fei Liu

Ook said:
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.

You might want to use the first one if the string _stuff has a large
memory footprint. You call it by

string * zoot;
zoot = getStuff();
 
J

Jim Langston

Ook said:
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.
 
O

Ook

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
private variables because that is how my first c++ instructor had us
do it <shrug>. Are there any generally accepted naming conventions for
variables in c++?
 
J

John Harrison

Hi, thanks both of your for the clairifcation. I use the _ to prefix
private variables because that is how my first c++ instructor had us
do it <shrug>. Are there any generally accepted naming conventions for
variables in c++?

Underscore to prefix class variables is perfectly legal C++. I use it
myself and I think it's a great convention. Jim is warning you that some
other uses of underscores in names are not legal. I never really
understood why the other illegal uses of underscore should count against
this legal use of underscore, but Jim isn't the only person to think
this way.

john
 
R

red floyd

John said:
Underscore to prefix class variables is perfectly legal C++. I use it
myself and I think it's a great convention. Jim is warning you that some
other uses of underscores in names are not legal. I never really
understood why the other illegal uses of underscore should count against
this legal use of underscore, but Jim isn't the only person to think
this way.

Because the next poor schmoe to maintain your code may refactor it out,
and leave the underscore in place in the global namespace.

Or you may get into the habit of a prefix on members, and have a member
such as _MyMember, which is not valid in any context.
 
J

John Harrison

red said:
Because the next poor schmoe to maintain your code may refactor it out,
and leave the underscore in place in the global namespace.

Class member variable into global variable is a pretty unusual
refactoring. I think I'd be worried about more than breaking the C++
naming rules.
Or you may get into the habit of a prefix on members, and have a member
such as _MyMember, which is not valid in any context.

The underscore is a prefix on members, I don't use it anywhere else.

john
 
G

Grizlyk

Ook said:
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.

It is easy to use

string getStuff(){ return _stuff; }

by the cost of possible perfomans lost. Read books about C++ and differences
between variables returned by value and variables returned by reference or
pointer.


--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
 
V

Victor Bazarov

Grizlyk said:
It is easy to use

string getStuff(){ return _stuff; }

And even probably

string getStuff() const { return _stuff; }
by the cost of possible perfomans lost. Read books about C++ and
differences between variables returned by value and variables
returned by reference or pointer.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top