Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Return by reference

Reply
Thread Tools

Return by reference

 
 
Michael
Guest
Posts: n/a
 
      10-13-2004
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-13-2004
Michael wrote:
> 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
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      10-13-2004
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
 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      10-13-2004

"Michael" <> wrote in message
news:ckk4t0$ohd$...
> 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!


 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      10-13-2004

"JKop" <> wrote in message
news:iPgbd.33355$...
> 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


that is cool. Thanks!


 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      10-14-2004
Michael wrote:

> 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.



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      10-14-2004
"Michael" <> wrote in message news:ckk4t0

> 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>: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!
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What to return -- object, reference or const reference Arv C++ 15 03-07-2008 09:15 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
difference between return &*i and return i; Ganesh Gella C++ 4 11-12-2004 04:28 PM
How do I return a return-code from main? wl Java 2 03-05-2004 05:15 PM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57