且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有相同名称和参数,不同返回类型的C ++成员函数

更新时间:2022-11-22 12:03:21

否,它无效,但是在您的示例中是有效的,因为最后一个 const 实际上是签名(隐藏的 Foo * this 第一个参数现在为 const Foo * this ).

no, it is not valid, but in your example it is, because the last const is actually part of the signature (the hidden Foo *this first parameter is now const Foo *this).

它用于以只读方式访问(获取const引用,方法是常量),或进行写入(获取非const引用,方法不是常量)

It is used to access in read-only (get const reference, the method is constant), or write (get non-const reference, the method is not constant)

在两种方法中都返回相同实体(常量或非常量)的引用仍然是一个不错的设计选择!

it's still a good design choice to return the reference of the same entity (constant or non-constant) in both methods of course!