Re: pass string by value because of "mean what you say"?
On Feb 23, 6:55 pm, SG <s.gesem...@gmail.com> wrote:
I think you meant
void setName(std::string const& newName) { namePtr =
&newName; }
std::string* namePtr;
I think both of you meant
const std::string* namePtr;
It's reasonable to use pass-by-reference-to-const in setName(). It'll
avoid an unnessecary copy. Using return-by-value has two advantages:
(a) the user can't use a const_cast to mess with your object's
internals and (b) it allows an object to store the name in something
other than a string object.
That seems a bit excessive to use return-by-value to protect the
underlying object. It isn't a bank full of money, after all; it isn't
even an object belonging to the author of the class. As a rule, one
shouldn't waste time protecting people from the consequences of overt
abuse. Leave that to Java! And a simple .c_str() will overcome
anything gained as far as alternative storage.
Kevin P. Barry
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]