returning reference of a member variable

Discussion in 'C++' started by Naren, Jun 7, 2006.

  1. Naren

    Naren Guest

    Hi ,
    Is it correct to return reference of a member variable.
    because it can be like we have already deleted the object and still
    holding the reference
    of member variable.


    A *a = new A;
    int & r = a.get(); // int& A::get() { //returning a member of class
    A}
    delete a;
    cout<<r;
    Naren, Jun 7, 2006
    #1
    1. Advertising

  2. Naren

    Rolf Magnus Guest

    Naren wrote:

    > Hi ,
    > Is it correct to return reference of a member variable.


    Yes.

    > because it can be like we have already deleted the object and still
    > holding the reference of member variable.


    Well, you have to ensure that this doesn't happen.

    > A *a = new A;
    > int & r = a.get(); // int& A::get() { //returning a member of class
    > A}
    > delete a;
    > cout<<r;


    That would invoke undefined behavior.
    Rolf Magnus, Jun 7, 2006
    #2
    1. Advertising

  3. Naren

    Greg Guest

    Naren wrote:
    > Hi ,
    > Is it correct to return reference of a member variable.
    > because it can be like we have already deleted the object and still
    > holding the reference
    > of member variable.
    >
    >
    > A *a = new A;
    > int & r = a.get(); // int& A::get() { //returning a member of class
    > A}
    > delete a;
    > cout<<r;


    Another problem with returning a non-const reference to a member is
    that the client can then use it to change the value of the object's
    member variable directly - instead of having to go through the proper
    channels (which would be the object's class interface).

    Greg
    Greg, Jun 7, 2006
    #3
  4. Naren

    Guest

    Rolf Magnus wrote:
    > Naren wrote:
    > ...
    > > A *a = new A;
    > > int & r = a.get(); // int& A::get() { //returning a member of class
    > > A}
    > > delete a;
    > > cout<<r;

    >
    > That would invoke undefined behavior.


    And what if I use
    int const& r = a.get(); //int const& A::get()

    will it still be undefined behavior?
    , Jun 8, 2006
    #4
  5. wrote:
    > Rolf Magnus wrote:
    >> Naren wrote:
    >> ...
    >>> A *a = new A;
    >>> int & r = a.get(); // int& A::get() { //returning a member of
    >>> class A}
    >>> delete a;
    >>> cout<<r;

    >>
    >> That would invoke undefined behavior.

    >
    > And what if I use
    > int const& r = a.get(); //int const& A::get()
    >
    > will it still be undefined behavior?


    What's different here? 'a' is not a temporary, A::get returns
    a reference, not a temporary. As soon as 'a' is destroyed, all
    of its contents is gone, and any reference to any part of 'a'
    becomes invalid. Using it has UB.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 8, 2006
    #5
    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. Replies:
    7
    Views:
    368
    Andrey Tarasevich
    Feb 14, 2005
  2. ww
    Replies:
    4
    Views:
    377
    James Kanze
    Oct 26, 2007
  3. tech
    Replies:
    3
    Views:
    369
    Federico Zenith
    Jun 18, 2008
  4. JustMe
    Replies:
    1
    Views:
    165
    Tassilo v. Parseval
    Aug 29, 2003
  5. A. Farber
    Replies:
    5
    Views:
    156
    A. Farber
    Aug 23, 2008
Loading...

Share This Page