Return by reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    #1

    Return by reference

    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


  • Victor Bazarov

    #2
    Re: Return by reference

    Michael wrote:[color=blue]
    > When can i return by reference?[/color]

    When the object a reference to which you're returning is still alive
    and well after the function returns.
    [color=blue]
    > 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?[/color]

    Yes, perfectly OK.
    [color=blue]
    > 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.[/color]

    Absolutely.

    V

    Comment

    • JKop

      #3
      Re: Return by reference

      Michael posted:
      [color=blue]
      > 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[/color]


      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("He llo!");
      //blah is now valid until the end of main


      std::string const &poo = FuncThatReturns StringByVALUE() ; //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

      Comment

      • Michael

        #4
        Re: Return by reference


        "Michael" <slick_mick_00@ hotmail.com> wrote in message
        news:ckk4t0$ohd $1@hercules.bti nternet.com...[color=blue]
        > 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[/color]
        if[color=blue]
        > I returned (Direction *4) as a tempory object would be created with local
        > scope then returning a reference to an invalid object.
        >
        > Thanks
        > Mike
        >
        >[/color]

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


        Comment

        • Michael

          #5
          Re: Return by reference


          "JKop" <NULL@NULL.NULL > wrote in message
          news:iPgbd.3335 5$Z14.13494@new s.indigo.ie...[color=blue]
          > Michael posted:
          >[color=green]
          > > 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[/color]
          >
          >
          > 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("He llo!");
          > //blah is now valid until the end of main
          >
          >
          > std::string const &poo = FuncThatReturns StringByVALUE() ; //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[/color]

          that is cool. Thanks!


          Comment

          • Ioannis Vranos

            #6
            Re: Return by reference

            Michael wrote:
            [color=blue]
            > 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.[/color]



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



            --
            Ioannis Vranos


            Comment

            • Siemel Naran

              #7
              Re: Return by reference

              "Michael" <slick_mick_00@ hotmail.com> wrote in message news:ckk4t0
              [color=blue]
              > 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[/color]
              if[color=blue]
              > I returned (Direction *4) as a tempory object would be created with local
              > scope then returning a reference to an invalid object.[/color]

              It's fine and done often, as in vector<T>::oper ator[] 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!
              }


              Comment

              Working...