C + + operator (overloaded operator) "Go"

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/xiangxiaodong/archive/2012/02/12/2348144.html

Operator is a C + + keyword that is used in conjunction with an operator to represent an operator function that should be understood as a function name as a whole operator=.

This is a method of the C + + extension operator function, although it looks strange, but it is understandable: on the one hand, the use of the operator is consistent with its original, on the other hand, extending its function only through the function of the way (C + +, "function" is implemented by the function).

First, why use operator overloading?
For all operators of the system, in general, only the basic data type and the class provided in the standard library are supported, for the user-defined class, if you want to support basic operations such as comparing size, judging equality, and so on, you need to define a specific implementation of this operator. For example, judging whether two people are the same size, our default rules are based on their age to compare, so, in the design of the person this class, we need to consider the operator = =, and, according to the analysis just now, the basis of the comparison should be ages. So why is it called overloading? This is because, at the time of the compiler implementation, we have provided the basic data type implementation version of this operator, but now his operand becomes the user-defined data type class, so the user is required to provide the implementation of the parameter version.

Second, how to declare an overloaded operator?
A: operator overloading is implemented as a class member function
The overloaded operator is declared in the body of the class, declared in the same way as a normal member function, except that his name contains the keyword operator, followed by a C + + pre-defined operator.
You can declare a predefined = = operator in the following way:
Class person{
Private
int age;
Public
person (int a) {
this->age=a;
}
inline bool operator = = (Const person &PS) const;
};
Here's how it's implemented:
inline bool person::operator== (const person &PS) const
{

if (this->age==ps.age)
return true;
return false;
}
The method is called as follows:
#include
using namespace Std;
int main ()
{

Person P1 (10);
Person P2 (20);
if (P1==P2) cout<< "The Age is equal!" < return 0;
}
Here, because operator = = is a member of the class person function, so the object P1,P2 can call the function, the above if statement, the equivalent of P1 call function = =, the P2 as a parameter of the function to the function, so as to achieve a comparison of two objects.

Overloaded operators have the following limitations:

(1) Only operators in C + + pre-defined operator set can be overloaded;

(2) for a built-in type operator, its predefined cannot be changed, should not be built-in type overloaded operators, such as, can not change the int type operator + meaning;

(3) Also cannot define other operators for the built-in data type;

(4) Only operators of class types or enumeration types can be overloaded;

(5) Overloaded operators cannot change their operator precedence;

(6) The overloaded operator cannot change the number of operands;

(7) In addition to the () operator, it is illegal to provide default arguments to other overloaded operators;

C + + operator (overloaded operator) "Go"

Related Article

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.