Overloaded operator "<" and friend property in C + +

Source: Internet
Author: User

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 + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.