操作符重载调用优先级
先粗略记一下。。。。
对同一个操作符同时实现成员函数重载和友元重载时,优先调用成员重载,当不存在成员重载时调用友元重载
1 #include "stdafx.h" 2 3 class CTest { 4 public: 5 CTest(int nValue) : nValue_(nValue) {} 6 7 friend bool operator ==(const CTest& t1, const CTest& t2) { 8 return t1.nValue_ == t2.nValue_; 9 } 10 11 bool operator == (const CTest& t) { 12 return nValue_ == t.nValue_; 13 } 14 15 private: 16 int nValue_; 17 }; 18 19 int _tmain(int argc, _TCHAR* argv[]) 20 { 21 CTest t1(1); 22 CTest t2(2); 23 bool bSame = t1 == t2; 24 bSame = t1.operator==(t2); 25 bSame = operator == (t1, t2); 26 return 0; 27 }