赋值运算符的参数必须是引用吗?
C++中重载类的赋值运算符时,其参数必须是引用吗?
例如,
class MyClass {
public:
...
MyClass & operator=(const MyClass &rhs);
...
}
可以吗
class MyClass {
public:
...
MyClass & operator=(const MyClass rhs);
...
}
?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一般来说,由您决定,没有必须。第一个变体是常见的且“规范的”,并且适用于任何赋值运算符实现。
当问题是速度时,我认为你应该阅读 这篇关于按值传递技术的文章。这意味着在某些情况下,按值传递比按 const 引用传递更有效。
还要提一下,您的第二个变体不需要
const
关键字,因为按值传递就像创建了副本一样,因此原始对象肯定不会更改。 >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.C++ 运算符重载指南 建议,赋值运算符获取 const 引用。根据该网站,原因是我们不想更改参数(因为 const),而只是更改运算符的左侧。因此,通过引用传递它可以节省时间。
它还指出了赋值运算符 - 运算符链接返回引用的原因。为了使
a = (b = 1)
正常工作,(b = 1)
必须返回一个可以赋值的引用 (=
) 到a
。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 (=
) toa
.您知道异常安全赋值的复制和交换习惯吗?
通过按值调用可以简化实现(在某些情况下还可以加快实现速度):
Do you know the copy and swap idiom for exception safe assignment?
The implementation can be simplified (and in some cases sped up) via call by value:
好吧,我遇到了这个问题,但找不到好的答案,所以我将分享我学到的东西。
你可以按值传递,这没有什么问题。 (正如您在问题中所示。)
但是我们通过 const 引用传递参数的原因是函数不会创建被调用值的实际副本。它被引用,所以它只是指向该值(无论它在哪里)记忆中。
这可以节省处理时间,特别是如果它是一个有数千个名字的大东西......
在这种情况下,节省的时间几乎为零。
对于 const,这可以确保函数的用户不会更改引用的值,因为它可能会更改,因为您可以访问内存中的原始位置,因为它是通过引用传递的。
如果你的函数定义实际上改变了 const 引用调用的参数的值,这将是一个编译器错误,它不会让你这样做。因为当你放置 const 时,你就告诉编译器这个值不能改变。
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.
重载赋值运算符的参数可以是任何类型,并且可以通过引用或按值传递(当然,如果该类型不可复制构造,那么显然它不能按值传递)。
因此,例如,您可以有一个采用
int
作为参数的赋值运算符:复制赋值运算符是赋值运算符的特殊情况。它是任何采用与类相同类型的赋值运算符,无论是通过值还是通过引用(引用可以是 const 或 volatile 限定的)。
如果您没有显式实现某种形式的复制赋值运算符,则编译器会隐式声明和实现该运算符。
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: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.