C++核心准则C.63:保证移动赋值运算符为非虚函数,参数类型为右值...

共 1128字,需浏览 3分钟

 ·

2020-01-11 23:23

1c70c564471aa4a3b1d3b02f80dec820.webp

C.63: Make move assignment non-virtual, take the parameter by &&, and return by non-const &
C.63:保证移动赋值运算符为非虚函数,参数类型为右值引用,返回值为常量引用类型

Reason(原因)

It is simple and efficient.

这样做简单且高效。


See(参见):

The rule for copy-assignment.

拷贝赋值规则。


相关链接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-copy-assignment




Enforcement(实施建议)

Equivalent to what is done for copy-assignment.

和拷贝赋值的情况时所做的相同。

  • (Simple) An assignment operator should not be virtual. Here be dragons!

    (简单)赋值运算符不应该是虚函数。那样做很危险。

  • (Simple) An assignment operator should return T& to enable chaining, not alternatives like const T& which interfere with composability and putting objects in containers.

    (简单)赋值运算符应该返回T&,这样才能实现连续赋值。不要改成类似const T&的类型,这样会影响组装性并妨碍将对象放进容器中。

  • (Moderate) A move assignment operator should (implicitly or explicitly) invoke all base and member move assignment operators.

    (中等)移动赋值运算符应该(隐式或显式)调用所有的基类和成员的移动赋值运算符。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c63-make-move-assignment-non-virtual-take-the-parameter-by--and-return-by-non-const-




觉得本文有帮助?请分享给更多人。

关注【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

浏览 31
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报