Should the assignment operator parameter be referenced?

When overloading a class assignment operator in C ++, should its parameter be a reference?

For example,

class MyClass { public: ... MyClass & operator=(const MyClass &rhs); ... } 

Could it be

 class MyClass { public: ... MyClass & operator=(const MyClass rhs); ... } 

?

Thanks!

+6
c ++ assignment-operator reference
source share
5 answers

The parameter of the overloaded assignment operator can be of any type and can be passed by reference or by value (well, if the type is not constructive, then it cannot be passed by value, obviously).

So, for example, you might have an assignment operator that takes an int as a parameter:

 MyClass& operator=(int); 

The copy assignment operator is a special case of the assignment operator. This is any assignment operator that takes the same type as a class, either by value or by reference (the link can be converted or unstable).

If you do not explicitly implement any form of copy assignment operator, then it is implicitly declared and implemented by the compiler.

+9
source share

As a rule, it's up to you, there is no must . The first option is general and "canonic" and is suitable for any implementation of the assignment operator.

When it comes to speed, I think you should read this article on value-based transmission techniques. This means that in some cases, passing by value will be more efficient than passing by constant reference.

In addition, your second option does not require the const keyword, because passing by value acts as if a copy had been created, so the original object will definitely not be changed.

+5
source share

C ++ operator overload guidelines show that an assignment operator gets a reference to a constant. According to the site, the reason is that we do not want to change the argument (starting with const), but only on the left side of the operator. Thus, it saves time to pass it by reference.

It also indicates the reason why the link is also returned by the assignment operator, a chain of operators. To get the job a = (b = 1) , it is necessary that (b = 1) return a link that can be assigned ( = ) to a .

+5
source share

Did you know that the idiom of copy and swap is to safely assign exceptions?

 MyClass& operator=(const MyClass& rhs) { MyClass copy(rhs); swap(copy); return *this; } 

Implementation can be simplified (and in some cases accelerated) through a call by value:

 MyClass& operator=(MyClass copy) { swap(copy); return *this; } 
+3
source share

Well, I had this problem, and I could not find a good answer, so I'm going to share what I found out.

You can go by value, there is nothing wrong with that. (as you showed in your question.)

But the reason we pass the parameter through the const link is because the function does not create an actual copy of the called value. Its reference, therefore, it simply points to this value, wherever it is in memory.

This saves processing time, especially if it is something big, having thousands of names ... In this case, the saved time will be almost nothing.

And for a constant that guarantees the user the function that the reference value will not be changed, because it can be changed, since you have access to the original location in memory, because it is passed by reference .. If your function definition actually changes the parameter value called in the const link, this will be a compiler error, this will not allow you to do this. because when you put const, you tell the compiler that this value cannot be changed.

-2
source share

All Articles