C + + operator overload function base and its value return status

Source: Internet
Author: User
Tags arithmetic arithmetic operators

Operator overloading is an important part of C + +, which makes programs easier to understand and simple operators use to make complex functions more intuitive.

It is natural for ordinary objects to use the arithmetic operators frequently to get them involved in the calculation, but for the object of the custom class, we cannot, in any case, prevent a program from writing the same code as the following.

Examples are as follows:

Class Test
{
Procedure omitted
}
int main ()
{
Test A,c;
C=a+a;
}

Of course, such code is not able to compile, C + + custom class of the arithmetic operation of the part reserved to the programmer, which is in line with the flexible features of C + +.

To implement such an operation in C + +, you must customize the operator overload function so that it can be fully and concretely worked.

The reader is reminded that the operator overloaded function of the custom class is also a function, and that all operators you overload do not change the priority of their operations because you define them, and the operation precedence of the custom operators follows the same order as the internal operators.

In addition, C + + also stipulates that some operators are not able to customize the overload, for example.,::,. * 、.->,?:.

Let's learn how to overload an operator, which is the form of an operator overloaded function:

return type operator operator symbol (parameter description)

{
//函数体的内部实现
}

The use of operator overloading functions is mainly divided into two forms, one is used as a friend function of the class, and the other is used as a member function of a class.

Let's take a look at the examples used as friend functions of a class:

Program Author: Junning
Site: www.cndev-lab.com
All manuscripts are copyrighted, if you want to reprint, please be sure that the famous source and author

#include <iostream>
using namespace Std;

Class Test
{
Public
Test (int a = 0)
{
Test::a = A;
}
Friend Test operator + (test&,test&);
Friend test& operator + + (test&);
Public
int A;
};
Test operator + (test& temp1,test& temp2)//+ operator overloaded function
{
cout<<temp1.a<< "|" <<temp2.a<<endl;//is here to observe the member component of the referenced object passed over.
Test result (temp1.a+temp2.a);
return result;
}
test& operator + + (test& temp)//++ operator overloaded functions
{
temp.a++;
return temp;
}
int main ()
{
Test A (100);
Test C=a+a;
cout<<c.a<<endl;
C + +;
cout<<c.a<<endl;
System ("pause");
}

In the example, we overloaded the addition operator and the automatic increment operator for the custom class test, and the overloaded operator completes the addition and increment operations of the same type of object.

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.