> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Daniel Glenfield
> Sent: Thursday, October 17, 2002 2:20 PM
> To: dev...@li...
> Subject: [Dev-C++] returning a constant string reference
>=20
>=20
>=20
>=20
> This is the code currently
> String GetName() const;
std::string GetName() const;
This returns a (temporary) object created using the copy constructor.
So for example if you have:
class something
{
string s;
public:
something(const string *n)
string GetName() const { return s; }
};
// ...
something so("Johnson");
string l=3Dso.GetName();
At the so.GetName() call
a temporary using the (default here) copy constructor
something::something(const something &) is called which is assigned to l
using the (default here) assignment something::operator=3D(const =
something
&). After the end of the expression the temporary is usually destroyed
(and not at the end of the scope).
>=20
> To make the function return a const string reference, do I changeit to
>=20
> String& GetName() const;
>=20
> or is it now returning a String object?
std::string &GetName();
It is returning a reference to an existing object (i assume you intend
to the data member).
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|