A temporary created for the return reference from Assignement Operator?

P

pvinodhkumar

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;
}
 
P

pvinodhkumar

(e-mail address removed) wrote in
@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?
 
T

Tobias Müller

(e-mail address removed) wrote in
@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
 
R

ruinerlee

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.
 
S

Stuart

(e-mail address removed) wrote in
@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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top