0

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.

1
  • @jrok I've mixed up the two operators. Nevermind. Commented Oct 9, 2013 at 20:17

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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.

Comments

0

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.

Comments

0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.