Operator overloading in C + + detailed parsing of _c language

Source: Internet
Author: User


One, what is operator overload
Operator overloading can be divided into two parts: "Operator" and "overload". When it comes to overloading, it's not strange, it's a compile-time polymorphism, and overloading can actually be divided into function overloads and operator overloads. The difference between operator overloading and function overloading is that operator overloading overloads must be operators. Let's take a look at the so-called operator overload first:

Copy Code code as follows:

#include <iostream>

using namespace Std;

int main ()
{
int a = 2, b = 3;
float c = 2.1f, d = 1.2f;
cout<< "A + b =" <<a+b<<endl;
cout<< "C + d =" <<c+d<<endl;
return 0;
}


We see that the operator "+" completes the addition calculation of float and int two types, which is the operator overload. The operator overloads of these built-in types have already been implemented, but what if the class we write ourselves to implement similar addition operations now?? For example, now there is a point class points, to achieve the addition of two points, the result is the horizontal ordinate to add, this time we need to write an operator overload function.
Copy Code code as follows:

#include <iostream>

using namespace Std;

Class Point
{
Double X;
Double y;
Public
Double get_x ()
{
return x;
}
Double get_y ()
{
return y;
}
Point (double X = 0.0, double y = 0.0): x (x), Y (y) {};
Point operator + (point P);
};
Overloaded operator "+"
Point Point::operator + (point P)
{
Double x = this->x + p.x;
Double y = this->y + p.y;
Point Tmp_p (X,y);
return tmp_p;
}
int main ()
{
Point P1 (1.2,3.1);
Point P2 (1.1,3.2);
Point p3 = P1+P2;
Cout<<p3.get_x () << "" <<p3.get_y () <<endl;
return 0;
}


Two ways of implementing operator overloading
Operator overloading can be implemented in two ways, through "friend Functions" or "class member functions."

1. The format of the friend function overload operator:

Copy Code code as follows:

Class name
{
Friend return type operator operator (formal parameter list);
};
To define a format outside of a class:
return type operator operator (parameter table)
{
function body
}

2. Class member functions implement the format of operator overloading:
Copy Code code as follows:

Class name
{
Public
return type operator operator (formal parameter list);
};
Define a format outside of a class
return type class Name:: operator operator (formal parameter list)
{
function body
}

To put it this way, it is still not enough to compare the two implementations, and we write the "+" and "-" overloads of the point classes in two ways. The code is as follows:
Copy Code code as follows:

#include <iostream>

Using Std::endl;
Using Std::cout;

Class Point
{
Double X;
Double y;
Public
Double get_x ()
{
return x;
}
Double get_y ()
{
return y;
}
Point (double X = 0.0, double y = 0.0): x (x), Y (y) {};
Friend point operator-(point p1,point P2);
Point operator + (point P);
};
Overloaded operator "-"
Point operator-(point p1,point p2)
{
Double x = p1.get_x ()-p2.get_x ();
Double y = p1.get_y ()-p2.get_y ();
Point P3 (x,y);
return p3;
}
Overloaded operator "+"
Point Point::operator + (point P)
{
Double x = this->x + p.x;
Double y = this->y + p.y;
Point Tmp_p (X,y);
return tmp_p;
}
int main ()
{
Point P1 (1.2,3.2);
Point P2 (1.1,3.1);
Point p3 = P1+P2;
Point P4 = operator-(P1,P2);
Cout<<p3.get_x () << "" <<p3.get_y () <<endl;
Cout<<p4.get_x () << "" <<p4.get_y () <<endl;
return 0;
}


I don't know what you see. When you overload the two-dollar operator "-" with the friend function, the formal argument is two, while the class member function is used with only one formal argument. Because the this pointer exists in the class member function, which is equivalent to a parameter, the class member implementation operator overload requires a less formal parameter than the original one, for example: Using a class member function to implement a unary operator "-", no arguments are required. It is for this reason that the operator overloads of the friend function implementations are limited, such as: [], (),-> and = cannot take advantage of the operator's overload with the friend function.

In the actual development process, the Monocular operator recommends overloading as a member function, while the binocular operator suggests overloading as a friend function. It is usually more convenient to load the lower binocular operator overload as a member function, but sometimes the binocular operator must be overloaded with a member function, such as an assignment operator =. Also, if you need to modify the internal state of the object, you can generally choose to use the class member function for modification.

Third, the principle of operator overloading
In this case, the operator overloading is quite simple, but the operator overload is also followed by some principles:

Only the existing C + + operators can be overloaded in 1.c++ and the user is not allowed to define new operators themselves.

Most of the operators in 2.c++ can be overloaded except for the member access operator., scope operator::, length operator sizeof, and conditional operator?:.

3. Operator overloading cannot change the number of Operations objects (operands) of an operator. For example: "+" is an operator that implements two operands, and is still the binocular operator after overloading.

4. Overloading cannot change the original precedence of the operator and the original binding.

5. Operator overloading cannot be all predefined basic data in C + +, in order to prevent users from modifying the nature of the operators used for basic type data.

Four, why operator overloading
about operator overloading follow so many principles, so why do operator overloading? Why don't I write an Add () function instead of operator + ()? Personal Sense C + + The reason to support operators is to unify operations with built-in data types, such as: C = a + b and c = Add (A,b), which seems to be more intuitive, obviously the former. At the same time, we want to manipulate the data types we define ourselves to be as convenient as manipulating the built-in data types int and double. Perhaps the example of this addition is a bit bad, now add the overloaded operator is [],<<,^,| and so on? What member function should we use instead?? What's the effect of replacing it? Can see at a glance what this function is going to do??

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.