C++核心准则C.63:保证移动赋值运算符为非虚函数,参数类型为右值引用,返回值为常量引用类型
发布日期:2021-07-01 05:26:41 浏览次数:2 分类:技术文章

本文共 1126 字,大约阅读时间需要 3 分钟。

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-

 


 

觉得本文有帮助?欢迎点赞并分享给更多的人。

阅读更多更新文章,请关注微信公众号【面向对象思考】

转载地址:https://oothinking.blog.csdn.net/article/details/104701336 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C++核心准则C.64:移动操作在完成移动之后,移动源对象应该保持有效状态
下一篇:C++核心准则C.62:保证拷贝赋值对自我赋值安全

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月14日 02时57分48秒