有没有人发现需要声明一个复制赋值操作符const的返回参数? [英] Has anyone found the need to declare the return parameter of a copy assignment operator const?
本文介绍了有没有人发现需要声明一个复制赋值操作符const的返回参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
副本赋值运算符具有通常的签名:
my_class& operator =(my_class const& rhs); my_class const& operator =(my_class const& rhs);
您只能定义一个或另一个,但不能同时定义。
解决方案使复制赋值的返回类型为非const引用的主要原因是它是标准中的Assignable的要求。 p>
如果你使返回类型为 const
引用,那么你的类将不能满足在任何标准库容器。
The copy assignment operator has the usual signature:
my_class & operator = (my_class const & rhs);
Does the following signature have any practical use?
my_class const & operator = (my_class const & rhs);
You can only define one or the other, but not both.
解决方案 The principle reason to make the return type of copy-assignment a non-const reference is that it is a requirement for "Assignable" in the standard.
If you make the return type a const
reference then your class won't meet the requirements for use in any of the standard library containers.
这篇关于有没有人发现需要声明一个复制赋值操作符const的返回参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文