简体   繁体   中英

Reason why the operator= usually returns a reference to an object in C++?

So for example:

const Ball& operator=(const Ball& other)
{
   // Irrelevant code

   return *this;
}

So I have heard that you return a reference to an object because of cascading:

Ball a,b,c;
// Unimportant initializations
a=b=c;

But why can't the method be something like:

const Ball operator=(const Ball& other)
{
   // Irrelevant code

   return *this;
}

Or this:

const Ball* operator=(const Ball& other)
{
   // Irrelevant code

   return this;
}

What if I do something like:

Ball *a, *b, *c;
// Unimportant initializations
a = b;

Would this work with both the methods I provided?

I apologize if these are dumb questions. I am still fairly new at C++. Any help would be nice.

The return type is convention, but normally it's Ball& not const Ball& . I think you can have anything there you like (void for instance, if you don't want to return anything at all). But it's best to have something that behaves like the assignment operator does with built in types. That way people won't be surprised how your assignment operator behaves.

This code

Ball *a, *b, *c;
// Unimportant initializations
a = b;

is just pointer assignment, it will always be legal but it has nothing to do with any assignment operator you define which is for Ball objects not Ball pointers.

Returning by value would work, but you make unnecessary copies.

Returning by pointer would prevent natural chaining, you'd need some weird thing like

a = *(b = c);

And

Ball *pa;
Ball *pb;

pa = pb;

will not use your user-defined assignment operator, the assignment operator for pointers is built-in and does pointer assignment.

If you have a const reference then you cannot assign to it. If you return a pointer, again, you assign the pointer value, not the object instance.

In most cases it makes sense to stick to what is called "int semantics" whereas you make your classes behave in the same manner int does. In particular, you want cascading to work.

If you have a reference, you can chain your operations, like this

  a.add(b).subtract(c);

But if you return a pointer or const, you can not do that.

You can define it this way:

const Ball operator=(const Ball& other)
{
   // Irrelevant code
   return *this;
}

However, you will be returning a temporary copy. The compiler will likely fix this for you when it optimizes. Additionally, chaining a = b = c when operator= takes a const Ball& , you are passing a reference to a temporary.

Returning a pointer will not work as you cannot chain, and you are dealing with different data types (pointers on the left hand side, const references on the right).

This snippet is legal:

int a;
(a = 2)++;

In one statement, we assign 2 to a and then increment a . This means that a = 2 must evaluate to a reference to a .

In order to replicate this behavior with user made types, the assignment operator needs to return a reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM