Sign in to follow this  

Returning a reference

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone show me a simple example where returning a reference would produce the wrong result. I know the difference between int foo() {...} and int& foo() {...} but I can't think of when I should not return a reference. Thanks for the help!

Share this post


Link to post
Share on other sites
Correct me if I'm wrong, because I don't do this kind of thing very often, but if you return a reference of a variable that is declared within that function (on the stack), the variable will be deallocated and the reference will be useless.
If you allocate something on the heap using new or malloc, that will work- the reference will still be good.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

class cMyFoo
{
public:
const cVector3& GetPosition() const;
cVector3 GetParentPosition() const;

private:
cVector3 m_Position;
cMyFoo *m_Parent;
};


const cVector3& cMyFoo::GetPosition() const
{
return m_Position;
}

cVector3 cMyFoo::GetParentPosition() const
{
if (m_Parent)
{
cVector3 pos;
pos = m_Position + m_Parent->GetPosition();
return pos;
}

return m_Position;
}



no the first GetPosition() method will return reference to member varaiable of cMyFoo class. This allows us not to make a copy of that variable. In the second method we are adding pour position and parent position using local variable.

This is very simplified...

Share this post


Link to post
Share on other sites
Ok I understand now. The important thing is not to return a local or temporary variable by reference because it may no longer be valid after the function goes out of scope, which means the caller of the function may get garbage.

Share this post


Link to post
Share on other sites
You should also avoid returning references to private class members, doing so effectively allows what declaring the variable private is supposed to disallow - outside modification of variables in a way in which you have no control over.

Share this post


Link to post
Share on other sites
Just to second Spudder - classes are there to encapsulate data and function together. Although possible and legal, if you start giving away references you would be breaking OO design rules - because you are taking away control of the internal data from being exlusively handled by its class methods.

Share this post


Link to post
Share on other sites

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this