By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Mar 9 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ook wrote:
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();
Mar 9 '07 #2

P: n/a
"Ook" <zo****@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.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.
Mar 9 '07 #3

P: n/a
Ook
On Mar 9, 1:44 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Ook" <zoo...@gmail.comwrote in message

news:11**********************@h3g2000cwc.googlegro ups.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
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++?

Mar 9 '07 #4

P: n/a
>>
>>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++?
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

Mar 9 '07 #5

P: n/a
John Harrison wrote:
>>>
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++?

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.
Mar 10 '07 #6

P: n/a
red floyd wrote:
John Harrison wrote:
>>>>
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++?

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.
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
Mar 10 '07 #7

P: n/a
Ook wrote:
>
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/
Mar 17 '07 #8

P: n/a
Grizlyk wrote:
Ook wrote:
>>
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; }
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 17 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.