Return by reference

Discussion in 'C++' started by Michael, Oct 13, 2004.

  1. Michael

    Michael Guest

    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
    Michael, Oct 13, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Oct 13, 2004
    #2
    1. Advertising

  3. Michael

    JKop Guest

    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
    JKop, Oct 13, 2004
    #3
  4. Michael

    Michael Guest

    "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!
    Michael, Oct 13, 2004
    #4
  5. Michael

    Michael Guest

    "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!
    Michael, Oct 14, 2004
    #5
  6. 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
    Ioannis Vranos, Oct 14, 2004
    #6
  7. Michael

    Siemel Naran Guest

    "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>::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!
    }
    Siemel Naran, Oct 14, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PvdK
    Replies:
    0
    Views:
    2,961
  2. wl
    Replies:
    2
    Views:
    566
    Dimitri Maziuk
    Mar 5, 2004
  3. Ganesh Gella
    Replies:
    4
    Views:
    344
    Stuart Gerchick
    Nov 12, 2004
  4. Greenhorn
    Replies:
    15
    Views:
    783
    Keith Thompson
    Mar 6, 2005
  5. Arv
    Replies:
    15
    Views:
    898
    James Kanze
    Mar 7, 2008
Loading...

Share This Page