Why friend overloaded operator is preferred to conversion operator in this case

Posted by skydoor on Stack Overflow See other posts from Stack Overflow or by skydoor
Published on 2010-03-13T18:07:26Z Indexed on 2010/03/13 18:15 UTC
Read the original article Hit count: 379

Filed under:
|

Hi I have a code like this, I think both the friend overloaded operator and conversion operator have the similar function. However, why does the friend overloaded operator is called in this case? What's the rules?

Thanks so much!

class A{

    double i;
public:
    A(int i):i(i) {}
    operator double () const { cout<<"conversion operator"<<endl;return i;}                            // a conversion operator
    friend bool operator>(int i, A  a);                            // a friend funcion of operator >
};

bool operator>(int i, A  a ){
    cout<<"Friend"<<endl;
    return i>a.i;
}
int main()
{
    A  aa(1);
     if (0 > aa){
         return 1;
      }
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about operator-overloading