Must parameter of assignment operator be reference?

流过昼夜 提交于 2021-02-07 07:00:54

问题


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

For example,

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

Can it be

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

?

Thanks!


回答1:


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

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

MyClass& operator=(int);

The copy assignment operator is a special case of an assignment operator. It is any assignment operator that takes the same type as the class, either by value or by reference (the reference may be const- or volatile-qualified).

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




回答2:


Generally, it's up to you to decide, there is no must. The first variant is common and "canonic" and is ok for any assignment operator implementation.

When the question is speed, I think you should read this article about passing-by-value technique. This means that in some cases passing by value would be more effective than passing by const reference.

Also to mention, your second variant doesn't need const keyword, because passing by value acts as if a copy was created, so the original object definitely won't be changed.




回答3:


C++ Operator Overloading Guidelines suggest, that the assignment operator gets a const reference. According to the site, the reason is that we do not want to change the argument (since const), but just the left hand side of the operator. Thus it saves time to pass it by reference.

It also points to the reason, why also a reference is returned by the assignment operator - operator chaining. In order to get a = (b = 1) working, it's necessary that (b = 1) returns a reference that can be assigned (=) to a.




回答4:


Do you know the copy and swap idiom for exception safe assignment?

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

The implementation can be simplified (and in some cases sped up) via call by value:

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



回答5:


Ok I had this problem and I couldn't find a good answer so I'm going to share what I learned.

You could pass by value there is nothing wrong with that. (as you showed in your question.)

But the reason we pass the parameter by const reference is so the function doesn't make an actual copy of the value being called in. Its referenced, so its just pointing at that value wherever it is in the memory.

This saves processing time especially if its something big that has thousands of names... In this case the time saved would be almost nothing.

And for the const, that ensures the user of the function that the referenced value is not going to be changed because it could be changed since you have access to the original location in the memory because its passed by reference.. If your function definition actually changes the value of the parameter being called in by const reference , it will be a compiler error, it wont let you do that. because when you put const, you are telling the compiler this value cannot be changed.



来源:https://stackoverflow.com/questions/2940596/must-parameter-of-assignment-operator-be-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!