C++核心准则C.161:对称运算使用非成员函数运算符
C.161: Use nonmember functions for symmetric operators
C.161:对称运算使用非成员函数运算符
Reason(原因)
If you use member functions, you need two. Unless you use a nonmember function for (say) ==, a == b and b == a will be subtly different.
如果使用成员函数的话,需要准备两个。除非你使用非成员函数实现==,否则a==b和b==a会有微妙的区别。
Example(示例)
bool operator==(Point a, Point b) { return a.x == b.x && a.y == b.y; }
Enforcement(实施建议)
Flag member operator functions.
提示成员函数操作符。
原文链接:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c161-use-nonmember-functions-for-symmetric-operators
觉得本文有帮助?请分享给更多人。
关注【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
评论