From: restor on
> Frankly, what you describe seems like a perfect fit for using shared_ptr
>
> Have your member be a shared_ptr<std::string>.
> And return shared_ptr<const std::string> from your getter function.

shared_ptr adds itself a visible overhead, and may even be slower than
returning string by value if the string has an embedded reference
counting.

Regards,
&rzej

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Hounsome on
On 3 Dec, 17:21, Stuart Golodetz
<sgolod...(a)NdOiSaPlA.pMiPpLeExA.ScEom> wrote:

> int main()
> {
> shared_ptr<X> x(new X);

The following line is NEVER a good idea.
I know a lot of people seem to think that a class should be written to
be idiot proof but idiots are so inventive that that is difficult - I
prefer to just not employ idiots.

> const std::string& r = x->s();
> x.reset();
>
> // BOOM
> std::cout << r << '\n';
>
> return 0;
>
> }


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]