C++核心準(zhǔn)則C.161:對(duì)稱(chēng)運(yùn)算使用非成員函數(shù)?運(yùn)算符

C.161: Use nonmember functions for symmetric operators
C.161:對(duì)稱(chēng)運(yùn)算使用非成員函數(shù)運(yùn)算符
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.
如果使用成員函數(shù)的話,需要準(zhǔn)備兩個(gè)。除非你使用非成員函數(shù)實(shí)現(xiàn)==,否則a==b和b==a會(huì)有微妙的區(qū)別。
Example(示例)
bool operator==(Point a, Point b) { return a.x == b.x && a.y == b.y; }
Enforcement(實(shí)施建議)
Flag member operator functions.
提示成員函數(shù)操作符。
原文鏈接:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c161-use-nonmember-functions-for-symmetric-operators
覺(jué)得本文有幫助?請(qǐng)分享給更多人。
關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_(kāi)發(fā),面向?qū)ο笏伎迹?/span>
評(píng)論
圖片
表情
