How to return by reference?

Discussion in 'C++' started by Bo Yang, Nov 29, 2006.

  1. Bo Yang

    Bo Yang Guest

    I know the below code is wrong:

    Object & test(){ return Object() ;}
    Object & v = test() ;

    the Object is just a temporary variable,
    so you the variable may be reference to
    nothing.

    And I try to this:

    Object & test () { return * new Object(); }
    Object & v = test () ;

    But how can I destroy the Object's memery?

    But, how to return reference safely?
    Is there any principle to return by reference?
     
    Bo Yang, Nov 29, 2006
    #1
    1. Advertising

  2. Bo Yang

    Ondra Holub Guest

    Bo Yang napsal:
    > I know the below code is wrong:
    >
    > Object & test(){ return Object() ;}
    > Object & v = test() ;
    >
    > the Object is just a temporary variable,
    > so you the variable may be reference to
    > nothing.
    >
    > And I try to this:
    >
    > Object & test () { return * new Object(); }
    > Object & v = test () ;
    >
    > But how can I destroy the Object's memery?
    >
    > But, how to return reference safely?
    > Is there any principle to return by reference?


    By reference is usually returned
    (1) *this (typically in overloaded operator=) or
    (2) some class member instance or
    (3) instance stored in some container

    The main rule is "Instance must not be local variable of function,
    which returns reference to it.

    class Test
    {
    public:
    Test(int data): data_(data) { }

    // This example for case (1)
    Test& operator=(const test& src)
    {
    data_ = src.data_;
    return *this;
    }

    // This is example for case (2)
    int& GetData() { return data_; }
    };
     
    Ondra Holub, Nov 29, 2006
    #2
    1. Advertising

  3. Bo Yang

    kwikius Guest

    Bo Yang wrote:
    > I know the below code is wrong:
    >
    > Object & test(){ return Object() ;}
    > Object & v = test() ;
    >
    > the Object is just a temporary variable,
    > so you the variable may be reference to
    > nothing.
    >
    > And I try to this:
    >
    > Object & test () { return * new Object(); }
    > Object & v = test () ;
    >
    > But how can I destroy the Object's memery?


    delete & v;

    means delete the variable that v is a reference to which is the
    unnamed Object created in test();


    > But, how to return reference safely?
    > Is there any principle to return by reference?


    Usually you will also pass the object into the function by reference:

    class Object{};
    Object & test(Object &);


    Object o;

    Object & v = test(o);

    v is 'another name' for o;

    regards
    Andy Little
     
    kwikius, Nov 29, 2006
    #3
  4. Bo Yang

    Bo Yang Guest

    Ondra Holub :
    > Bo Yang napsal:
    >> I know the below code is wrong:
    >>
    >> Object & test(){ return Object() ;}
    >> Object & v = test() ;
    >>
    >> the Object is just a temporary variable,
    >> so you the variable may be reference to
    >> nothing.
    >>
    >> And I try to this:
    >>
    >> Object & test () { return * new Object(); }
    >> Object & v = test () ;
    >>
    >> But how can I destroy the Object's memery?
    >>
    >> But, how to return reference safely?
    >> Is there any principle to return by reference?

    >
    > By reference is usually returned
    > (1) *this (typically in overloaded operator=) or
    > (2) some class member instance or
    > (3) instance stored in some container
    >
    > The main rule is "Instance must not be local variable of function,
    > which returns reference to it.


    And when I implement a '-' operator in a class,
    I must to do

    Test & operator -( Test & right ) ;

    How to implement this function then ?
    I must to new a Test in this function , mustn't I ?
    >
    > class Test
    > {
    > public:
    > Test(int data): data_(data) { }
    >
    > // This example for case (1)
    > Test& operator=(const test& src)
    > {
    > data_ = src.data_;
    > return *this;
    > }
    >
    > // This is example for case (2)
    > int& GetData() { return data_; }
    > };
    >
     
    Bo Yang, Nov 29, 2006
    #4
  5. Bo Yang

    dasjotre Guest

    Bo Yang wrote:
    > I know the below code is wrong:
    >
    > Object & test(){ return Object() ;}
    > Object & v = test() ;
    >
    > the Object is just a temporary variable,
    > so you the variable may be reference to
    > nothing.
    >
    > And I try to this:
    >
    > Object & test () { return * new Object(); }
    > Object & v = test () ;
    >
    > But how can I destroy the Object's memery?
    >
    > But, how to return reference safely?
    > Is there any principle to return by reference?


    you can return Object by value

    Object test(){ return Object(); }

    and then bind the return to reference on use

    Object & obj = test();

    If carefull, you can avoid copying

    or, if Object is too big for stack, use

    std::auto_ptr<Object> test(){ return std::auto_ptr<Object>(new Object);
    }
     
    dasjotre, Nov 29, 2006
    #5
  6. Bo Yang

    Rolf Magnus Guest

    Bo Yang wrote:

    >> By reference is usually returned
    >> (1) *this (typically in overloaded operator=) or
    >> (2) some class member instance or
    >> (3) instance stored in some container
    >>
    >> The main rule is "Instance must not be local variable of function,
    >> which returns reference to it.

    >
    > And when I implement a '-' operator in a class,
    > I must to do
    >
    > Test & operator -( Test & right ) ;


    Usually, you let that operator take a const reference as argument and return
    an object instead of a reference.

    > How to implement this function then ?
    > I must to new a Test in this function , mustn't I ?


    No. Well, you can, but shouldn't. You simply don't return by reference.
     
    Rolf Magnus, Nov 29, 2006
    #6
  7. Bo Yang

    Bo Yang Guest

    Rolf Magnus :
    > Bo Yang wrote:
    >
    >>> By reference is usually returned
    >>> (1) *this (typically in overloaded operator=) or
    >>> (2) some class member instance or
    >>> (3) instance stored in some container
    >>>
    >>> The main rule is "Instance must not be local variable of function,
    >>> which returns reference to it.

    >> And when I implement a '-' operator in a class,
    >> I must to do
    >>
    >> Test & operator -( Test & right ) ;

    >
    > Usually, you let that operator take a const reference as argument and return
    > an object instead of a reference.


    I have take a look at the FAQ and found
    the return type is usually a value but a reference.
    I understand the principle, return by reference only
    if the object you return is all at your control.

    Thank you!
    >
    >> How to implement this function then ?
    >> I must to new a Test in this function , mustn't I ?

    >
    > No. Well, you can, but shouldn't. You simply don't return by reference.
    >
     
    Bo Yang, Nov 29, 2006
    #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:
    3,002
  2. wl
    Replies:
    2
    Views:
    606
    Dimitri Maziuk
    Mar 5, 2004
  3. Ganesh Gella
    Replies:
    4
    Views:
    371
    Stuart Gerchick
    Nov 12, 2004
  4. Greenhorn
    Replies:
    15
    Views:
    884
    Keith Thompson
    Mar 6, 2005
  5. Arv
    Replies:
    15
    Views:
    952
    James Kanze
    Mar 7, 2008
Loading...

Share This Page