有没有人发现需要声明复制赋值运算符 const 的返回参数? [英] Has anyone found the need to declare the return parameter of a copy assignment operator const?

查看:21
本文介绍了有没有人发现需要声明复制赋值运算符 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.

推荐答案

将复制赋值的返回类型设为非常量引用的主要理由是它是标准中对Assignable"的要求.

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.

>

如果您将返回类型设为 const 引用,那么您的类将无法满足在任何标准库容器中使用的要求.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆