Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A temporary created for the return reference from Assignement Operator?

Reply
Thread Tools

A temporary created for the return reference from Assignement Operator?

 
 
pvinodhkumar@gmail.com
Guest
Posts: n/a
 
      06-29-2013
class A
{
A();
A& operator = (const A& aObject_in);
int x;
int y;
};


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

A& A: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;
}

 
Reply With Quote
 
 
 
 
Í÷ Tiib
Guest
Posts: n/a
 
      06-29-2013
On Saturday, 29 June 2013 14:08:00 UTC+3, (E-Mail Removed) wrote:
> return *this; // A temporary created here?


No. Unary operator* returns reference.
 
Reply With Quote
 
 
 
 
pvinodhkumar@gmail.com
Guest
Posts: n/a
 
      06-29-2013
On Saturday, June 29, 2013 7:20:32 PM UTC+5:30, Paavo Helde wrote:
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:ea56215d-9397-4a7f-a37b-fc9c6b0e3bf1
>
> @googlegroups.com:
>
>
>
> > A& A: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?
 
Reply With Quote
 
Tobias M├╝ller
Guest
Posts: n/a
 
      06-29-2013
<(E-Mail Removed)> wrote:
> On Saturday, June 29, 2013 7:20:32 PM UTC+5:30, Paavo Helde wrote:
>> (E-Mail Removed) wrote in news:ea56215d-9397-4a7f-a37b-fc9c6b0e3bf1
>>
>> @googlegroups.com:
>>
>>
>>
>>> A& A: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
 
Reply With Quote
 
ruinerlee@gmail.com
Guest
Posts: n/a
 
      06-29-2013
On Saturday, June 29, 2013 7:08:00 PM UTC+8, (E-Mail Removed) wrote:
> A& A: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.
 
Reply With Quote
 
Stuart
Guest
Posts: n/a
 
      06-30-2013
On 06/29/13, (E-Mail Removed) wrote:
> On Saturday, June 29, 2013 7:20:32 PM UTC+5:30, Paavo Helde wrote:
>> (E-Mail Removed) wrote in news:ea56215d-9397-4a7f-a37b-fc9c6b0e3bf1
>>
>> @googlegroups.com:
>>
>>
>>
>>> A& A: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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture the ôsameö RGB images for the same scene using EDSDK? azuring@gmail.com Digital Photography 0 04-28-2013 12:08 PM
Re: Is there any way to get the raw image data from a Nikon Coolpix S8200 P&S camera? Tony Cooper Digital Photography 16 04-17-2013 07:49 PM
silly question about Running a script from the command line A.Rock Python 0 04-10-2013 11:21 AM
Re: Is there any way to get the raw image data from a Nikon Coolpix S8200 P&S camera? nospam Digital Photography 0 04-09-2013 07:42 PM
Looking for assignement operator Alexander Eisenhuth Python 17 10-25-2006 09:12 AM



Advertisments