Return by reference

M

Michael

Ok,
When can i return by reference? For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;


};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous, eg if
I returned (Direction *4) as a tempory object would be created with local
scope then returning a reference to an invalid object.

Thanks
Mike
 
V

Victor Bazarov

Michael said:
When can i return by reference?

When the object a reference to which you're returning is still alive
and well after the function returns.
For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;


};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK?

Yes, perfectly OK.
I appreciate that in nots of cases this can be dangerous, eg if
I returned (Direction *4) as a tempory object would be created with local
scope then returning a reference to an invalid object.

Absolutely.

V
 
J

JKop

Michael posted:
Ok,
When can i return by reference? For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;


};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous,
eg if I returned (Direction *4) as a tempory object would be created
with local scope then returning a reference to an invalid object.

Thanks
Mike


You can use a reference where you can use a pointer.

Your code in the above is valid (save for a typo or two).


There are also other things you can do with references:

int main()
{
std::string const &blah = std::string("Hello!");
//blah is now valid until the end of main


std::string const &poo = FuncThatReturnsStringByVALUE(); //Note: by
value
//poo is now valid until the end of main
}

The above is referred (no pun intended) to as "binding a temporary to a
reference".


-JKop


-JKop
 
M

Michael

Michael said:
Ok,
When can i return by reference? For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;


};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous, eg if
I returned (Direction *4) as a tempory object would be created with local
scope then returning a reference to an invalid object.

Thanks
Mike

Sorry I didn't include all the mv3 operators but one of then allows me to
multiply by floats!
 
M

Michael

JKop said:
Michael posted:



You can use a reference where you can use a pointer.

Your code in the above is valid (save for a typo or two).


There are also other things you can do with references:

int main()
{
std::string const &blah = std::string("Hello!");
//blah is now valid until the end of main


std::string const &poo = FuncThatReturnsStringByVALUE(); //Note: by
value
//poo is now valid until the end of main
}

The above is referred (no pun intended) to as "binding a temporary to a
reference".


-JKop


-JKop

that is cool. Thanks!
 
I

Ioannis Vranos

Michael said:
Ok,
When can i return by reference? For the sake of simplicity:
lets say I have a class MLine:

class mv3
{
public:
float x,y,z;


};

class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous, eg if
I returned (Direction *4) as a tempory object would be created with local
scope then returning a reference to an invalid object.



Yes, but don't do it with regular functions by returning temporaries.
 
S

Siemel Naran

Michael said:
class MLine
{
mv3 PtOnLine;
mv3 Direction

public:
MLine();
const mv3& GetDirection() const
{
return Direction;
}

};

is this OK? I appreciate that in nots of cases this can be dangerous, eg if
I returned (Direction *4) as a tempory object would be created with local
scope then returning a reference to an invalid object.

It's fine and done often, as in vector<T>::eek:perator[] which returns a T& or
const T&. Be aware of code like this and don't write it:

const mv3& f() {
MLine m;
return m.GetDirection();
}

int main() {
cout << f().x; // crash, memory access violation!
}
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top