演算子オーバーロードの優先
同じシグネチャの演算子オーバーロードをメンバ関数とfriend関数で行った場合
どちらが優先されるのか気になったので調べてみました。
#include <iostream> using namespace std; class Hoge { public: bool operator==(const int a) { return true; } friend bool operator==(const Hoge& h1, const int a) { return false; } }; int main() { Hoge h; if (h == 3) { cout << "member function" << endl; } else { cout << "friend function" << endl; } }
結果:
member function
だそうです。
定義順序を逆にしても変化無し。