The first is defined as the member class overload operator, the second is the nonmember overload operator. When the Nonmember function accesses the private member private: int m_nCents; , friend must be added. Even I go to public:int m_nCents; , he does not work. friend seems to be the rule, not because of limiting access to membership. But you can move the Nonmember statement from the class of the class and access the public member variable. Any better idea? I feel embarrassed. I think that the ALL Nonmember operator (has the same parameter number with the operand and cannot be called as a member function) should be declared as friend in the body class.
> is a binary operator that has two parameters for each operand. You have found that the member > class overload operator has only an explicit parameter, one and an implicit this parameter. An operator without a member must have two.
class Cents{ private: int m_nCents; public: Cents(int nCents) : m_nCents(nCents) { } friend bool operator>(Cents &c1, Cents&c2); //Nomember bool operator<(Cents &c1); //class member }; bool operator>(Cents &c1, Cents&c2) // <--- why friend? { //cout << "in >" << endl; return (c1.m_nCents > c2.m_nCents) ? true: false; } bool Cents::operator<(Cents &c1) { //cout << "in <" << endl; return (this->m_nCents < c1.m_nCents) ? true: false; } int main(){ //nomember //if(poor.operator>(rich)) //compiler error if(poor > rich){ cout << "oh yeal!" << endl; } else { cout << "oh no!" << endl; } //member //if(poor.operator<(rich)) //can call this way if(poor.operator<(rich)){ cout << "oh yeal!" << endl; } else { cout << "oh no!" << endl; } }
I am moving implementations from the body of the class. Now you can see that the member operator of the class has the qualifier Cents:: as a member function.
source share