Overloaded operators in a class do not need to be member functions, or they can be declared as friend functions.
classperson{ Public://as a member function of a class, overloaded operator arguments require only the right action value to BOOL operator< (Constperson&Arg);Private: intA;};//Class name qualification is required for implementation BOOLPerson::operator< (Constperson&Arg) { if( ( This->a) <arg.a)return true; Else return false;} Person A1,A2;BOOLBRet = A1 < A2; That's right
Class Person
{
Public:
as a member function of a class, overloaded operator arguments require only the right action value to
friend booloperator< (Const person&Arg1,Const person& arg 2);
Private:
IntA
};
//Class name qualification is not required for implementation
bool operator< (const person& arg1, const person& arg2)
{
if ( arg1.a < arg2.a)
return true;
Else
return false;
}
Person A1,A2;
BOOL BRet = A1 < A2; That's right
In this example 1, the operator overloads the function as a member function of the class, and you can invoke the internal private protected member when using the object of that class.
In Example 2, the operator overload function, although not a member function of a class, is declared as a friend function of the class, and can also invoke a private protected member inside the class.
However, if you declare an overloaded function as a protection or private function, the overloaded function cannot be called when you use an object of that class, that is, if you write the overloaded function as a member function or a friend function, and it is a public property, there is no difference in using the operator, but if it is protected or private, Then outside you can't use the operator:
classperson{Protect: //as a member function of a class, overloaded operator arguments require only the right action value to BOOL operator< (Constperson&Arg);Private: intA;};//Class name qualification is required for implementation BOOLPerson::operator< (Constperson&Arg) { if( ( This->a) <arg.a)return true; Else return false;} Person A1,A2;BOOLBRet = A1 < A2; Error
classperson{Protect: //as a member function of a class, overloaded operator arguments require only the right action value tofriendBOOL operator< (Constperson& Arg1,Constperson&arg2);Private: intA;};//class name qualification is not required for implementation BOOL operator< (Constperson& Arg1,Constperson&arg2) { if(Arg1.a <arg2.a)return true; Else return false;} Person A1,A2;BOOLBRet = A1 < A2; Error
Overloaded operator "<" and friend property in C + +