A temporary created for the return reference from Assignement Operator?

Discussion in 'C++' started by pvinodhkumar@gmail.com, Jun 29, 2013.

  1. Guest

    class A
    {
    A();
    A& operator = (const A& aObject_in);
    int x;
    int y;
    };


    A::A()
    {
    x = 0;
    y = 0;
    }

    A& A::eek:perator=(const A& aObject_in)
    {
    if(this != &aObject_in)
    {
    x = aObject_in.x;
    y = aObject_in.y;
    }

    return *this; // A temporary created here?
    }

    int main()
    {
    A a1;
    A a2;
    a2 = a1;
    }
     
    , Jun 29, 2013
    #1
    1. Advertising

  2. Öö Tiib Guest

    On Saturday, 29 June 2013 14:08:00 UTC+3, wrote:
    > return *this; // A temporary created here?


    No. Unary operator* returns reference.
     
    Öö Tiib, Jun 29, 2013
    #2
    1. Advertising

  3. Guest

    On Saturday, June 29, 2013 7:20:32 PM UTC+5:30, Paavo Helde wrote:
    > wrote in news:ea56215d-9397-4a7f-a37b-fc9c6b0e3bf1
    >
    > @googlegroups.com:
    >
    >
    >
    > > A& A::eek:perator=(const A& aObject_in)

    >
    > > {

    >
    > > [...]

    >
    > > return *this; // A temporary created here?

    >
    > > }

    >
    >
    >
    > No, you have declared the function to return a reference so no temporary is
    >
    > created, just a reference to the this object is returned. Look up C++
    >
    > references in your C++ book.
    >
    >
    >
    > hth
    >
    > Paavo


    int main()
    {
    A a1;
    A a2;
    a2 = a1;
    }
    Let us examine the stament a2=a1;
    - Assignement operator returns a reference here
    - Then, A2 object is assigned with a reference returned?
    What happens to the previous object a2 here?
    When we say copying happens through the assignment operator things look ok.
    However there is already an a2 object, and one more reference is returned from the assignement.

    Will it be correct if we say, the returned reference is nothing but the reference to the a2 object itself as assignement actually initiated on a2?
     
    , Jun 29, 2013
    #3
  4. <> wrote:
    > On Saturday, June 29, 2013 7:20:32 PM UTC+5:30, Paavo Helde wrote:
    >> wrote in news:ea56215d-9397-4a7f-a37b-fc9c6b0e3bf1
    >>
    >> @googlegroups.com:
    >>
    >>
    >>
    >>> A& A::eek:perator=(const A& aObject_in)

    >>
    >>> {

    >>
    >>> [...]

    >>
    >>> return *this; // A temporary created here?

    >>
    >>> }

    >>
    >>
    >>
    >> No, you have declared the function to return a reference so no temporary is
    >>
    >> created, just a reference to the this object is returned. Look up C++
    >>
    >> references in your C++ book.
    >>
    >>
    >>
    >> hth
    >>
    >> Paavo

    >
    > int main()
    > {
    > A a1;
    > A a2;
    > a2 = a1;
    > }
    > Let us examine the stament a2=a1;
    > - Assignement operator returns a reference here
    > - Then, A2 object is assigned with a reference returned?
    > What happens to the previous object a2 here?
    > When we say copying happens through the assignment operator things look ok.
    > However there is already an a2 object, and one more reference is
    > returned from the assignement.
    >
    > Will it be correct if we say, the returned reference is nothing but the
    > reference to the a2 object itself as assignement actually initiated on a2?


    You are confusing objects and values here. If you assign an object to
    another, you only assign the _value_ of that object.
    That means, a2 always remains the same object, but changes its value.

    Consider the following snippet. It uses objects of some imaginary 'String'
    class, but it's valid for all other objects:

    String s1 = "aa";
    // s1 is an object
    // s1 has the value "aa"
    String s2 = "bb";
    // s2 is an object
    // s2 has the value "bb"
    String& r1 = s1;
    // r1 is a reference to s1
    // r1 has the value "aa"
    String& r2 = s2;
    // r2 is a reference to s2
    // r2 has the value "bb"
    s2 = s1;
    // s2 is still the same object
    // s2 has now the value "aa"
    // IMPORTANT: r2 is still a reference to s2 and has now also the value
    "aa"!
    r2 = "cc"
    // r2 has now the value "cc"
    // r2 is STILL a reference to s2
    // s2 has now also the value "cc"
    // s1 and r1 have still the value "aa"

    You can see that assignment only changes values, objects retain their
    identity and references always point to the same object, even if the value
    of that object changes.

    Tobi
     
    Tobias Müller, Jun 29, 2013
    #4
  5. Guest

    On Saturday, June 29, 2013 7:08:00 PM UTC+8, wrote:
    > A& A::eek:perator=(const A& aObject_in)
    >
    > {
    >
    > if(this != &aObject_in)
    >
    > {
    >
    > x = aObject_in.x;
    >
    > y = aObject_in.y;
    >
    > }
    >
    >
    >
    > return *this; // here *this is used to initiate a reference that is stored as return value
    >
    > }
    >
    >
    >
    > int main()
    >
    > {
    >
    > A a1;
    >
    > A a2;
    >
    > a2 = a1; //see below
    >
    > }

    a2 = a1 is identical to a2.operator=(a1);
    a function get called on a2, a1 is the parameter, return value is not used.
    so no additional copies here.
    if you write:
    A a3;
    a3 = (a2=a1);
    it's identical to:
    a3.operator(a2.operator(a1));
    there are 2 copies.
     
    , Jun 29, 2013
    #5
  6. Stuart Guest

    Re: A temporary created for the return reference from AssignementOperator?

    On 06/29/13, wrote:
    > On Saturday, June 29, 2013 7:20:32 PM UTC+5:30, Paavo Helde wrote:
    >> wrote in news:ea56215d-9397-4a7f-a37b-fc9c6b0e3bf1
    >>
    >> @googlegroups.com:
    >>
    >>
    >>
    >>> A& A::eek:perator=(const A& aObject_in)

    >>
    >>> {

    >>
    >>> [...]

    >>
    >>> return *this; // A temporary created here?

    >>
    >>> }

    >>
    >>
    >>
    >> No, you have declared the function to return a reference so no temporary is
    >>
    >> created, just a reference to the this object is returned. Look up C++
    >>
    >> references in your C++ book.
    >>
    >>
    >>
    >> hth
    >>
    >> Paavo

    >
    > int main()
    > {
    > A a1;
    > A a2;
    > a2 = a1;
    > }
    > Let us examine the stament a2=a1;
    > - Assignement operator returns a reference here


    That's right. The assignment operator will return a reference to a2, not
    to a1. However, this reference is not used by any other statement, so
    the returned reference will be ignored.

    > - Then, A2 object is assigned with a reference returned?


    Nope. a2 will be assigned through the reference to a1. This reference is
    passed into as operator=. This is easier to see if you use the following
    syntax:
    a2.operator=(a1);
    which is semantically equivalent to the statement
    a2 = a1;

    > What happens to the previous object a2 here?


    a2 stays where it was before, there is no movement in memory or
    something else.

    > When we say copying happens through the assignment
    > operator things look ok.
    > However there is already an a2 object, and one more
    > reference is returned from the assignement.


    Yes. Returning a reference does not mean that some new object has to be
    created. In essence, a reference is just a pointer that can never be
    null. So operator= returns the address of the variable that just got a
    new value. Why this is so, I can only speculate. Probably so that one
    can write such assignment chains like a = b = c, but in my 10 years as
    professional programmer (eight of them C++), I came never close to
    writing such a statement.

    > Will it be correct if we say, the returned reference is
    > nothing but the reference to the a2 object itself
    > as assignement actually initiated on a2?


    Badly phrased, but I think that you got the idea.

    Regards,
    Stuart
     
    Stuart, Jun 30, 2013
    #6
    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. Ken Varn
    Replies:
    0
    Views:
    298
    Ken Varn
    May 4, 2004
  2. yuraukar
    Replies:
    17
    Views:
    585
    Tom Widmer
    Oct 7, 2004
  3. Alexander Eisenhuth

    Looking for assignement operator

    Alexander Eisenhuth, Oct 17, 2006, in forum: Python
    Replies:
    17
    Views:
    492
    Bruno Desthuilliers
    Oct 25, 2006
  4. Replies:
    4
    Views:
    375
  5. jean
    Replies:
    4
    Views:
    134
    James Britt
    May 20, 2005
Loading...

Share This Page