Reference variable question?

S

Surendra Singhi

Hello,

Say I have a function 'func1', which calls an internal function int_func1,
which returns a reference to an object.

Now this object contains a string, is it safe for my function 'func1' to
return a pointer to this string, or should I copy the string somewhere else
and then return a pointer to the string?

Thanks.
--
Surendra Singhi
http://www.public.asu.edu/~sksinghi/index.html

,----
| By all means marry; if you get a good wife, you'll be happy. If you
| get a bad one, you'll become a philosopher.
| -- Socrates
`----
 
J

Jacques Labuschagne

Surendra said:
Hello,

Say I have a function 'func1', which calls an internal function int_func1,
which returns a reference to an object.

Now this object contains a string, is it safe for my function 'func1' to
return a pointer to this string, or should I copy the string somewhere else
and then return a pointer to the string?

Thanks.

Returning a pointer/reference to the string is only bad if
(a) the object is destroyed (or causes the string to be destroyed)
before the rest of your code is finished using the string, or
(b) another call to that function can make the object change the
string's value, which might give your other pointers/references to the
string a value you didn't expect.

So what's the lifetime of your object and how do you intend to use these
functions? From a library design point of view it's probably easier all
round if you return a copy, but there are always exceptions to a rule.

Jacques.
 
R

red floyd

Surendra said:
Hello,

Say I have a function 'func1', which calls an internal function int_func1,
which returns a reference to an object.

Now this object contains a string, is it safe for my function 'func1' to
return a pointer to this string, or should I copy the string somewhere else
and then return a pointer to the string?


Depends on what int_func1 returns. It may not even be safe for func1 to
play with said string.

Example:

// THIS IS BAD
#include <string>
using std::string;
class myclass()
{
public:
string s;
};

myclass& int_func1()
{
myclass c;
c.s = string("I am evil");
return c;
}

string func1()
{
myclass& c = func1();
string::size_type l = c.s.length(); // UNDEFINED BEHAVIOR
return c.s; // UNDEFINED BEHAVIOR
}
 
S

Surendra Singhi

Hello,
Thanks to everyone who replied.

Jacques Labuschagne said:
Returning a pointer/reference to the string is only bad if
(a) the object is destroyed (or causes the string to be destroyed)
before the rest of your code is finished using the string, or
(b) another call to that function can make the object change the
string's value, which might give your other pointers/references to
the string a value you didn't expect.

So what's the lifetime of your object and how do you intend to use
these functions? From a library design point of view it's probably
easier all round if you return a copy, but there are always exceptions
to a rule.

I cannot be sure about the lifetime of the objects.

I am designing a library, and currently I am copying the strings content and
then returning a pointer to the copied area, the receiver needs to bother
about deallocating.

Actually, I am working on a 'C' wrapper for wxWidgets, so just wanted to
conform whether I am doing the right thing.

Another related question, when I call a function like the one given below and
assign it to a variable say 'xyz=win.GetTitle()' then should I delete the
returned string 'delete xyz' or will this be automatically done by C++.

,----
|
| wxWindow::GetTitle
|
| virtual wxString GetTitle()
`----

Thanks once again.

--
Surendra Singhi
http://www.public.asu.edu/~sksinghi/index.html

,----
| By all means marry; if you get a good wife, you'll be happy. If you
| get a bad one, you'll become a philosopher.
| -- Socrates
`----
 
J

John Harrison

Surendra said:
Hello,
Thanks to everyone who replied.




I cannot be sure about the lifetime of the objects.

I am designing a library, and currently I am copying the strings content and
then returning a pointer to the copied area, the receiver needs to bother
about deallocating.

Actually, I am working on a 'C' wrapper for wxWidgets, so just wanted to
conform whether I am doing the right thing.

Another related question, when I call a function like the one given below and
assign it to a variable say 'xyz=win.GetTitle()' then should I delete the
returned string 'delete xyz' or will this be automatically done by C++.

,----
|
| wxWindow::GetTitle
|
| virtual wxString GetTitle()
`----

Thanks once again.

You only delete pointers (and only pointers that point at allocated
memory) but xyz appears to be an object (but I don't know the definition
of wxString, it could be a typedef for a pointer).

So with all those caveats I would say no, assuming wxString is some sort
of class type then xyz's destructor will be called when xyz goes out of
scope and you don't have do anything.

john
 

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,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top