operator keywords in C + + (overloaded operators)

Source: Internet
Author: User

From:https://www.cnblogs.com/wangduo/p/5561922.html

This article is for your own use only

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:

1 classperson{2 Private:3     intAge ;4      Public:5Personinta) {6         This->age=A;7     }8InlineBOOL operator== (ConstPerson &ps)Const;9};

Here's how it's implemented:

1 bool Person::operator= = (constconst2{3      if (this->age==ps.age)4         returntrue; 5      return false ; 6 }

The method is called as follows:

1 #include2 using namespacestd;3 intMain ()4 {5Person P1 (Ten);6Person P2 ( -);7   if(P1==P2) cout<< "The Age isequal! " <return 0;8}

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.

B: operator overloading implemented as a non-class member function (global function)
For global overloaded operators, the arguments that represent the left operand must be explicitly specified. For example:

1 #include2 #include3 using namespacestd;4 class Person5 {6      Public:7     intAge ;8      Public:9 };Ten  One BOOL operator= = (personConst&AMP;P1, personConst&p2) A //satisfies the requirement, the type of the operand to be done is displayed specified - { -     if(p1.age==p2.age) the     return true; -     return false; - } - intMain () + { - Person Rose; + Person Jack; ARose.age= -; atJack.age= at; -     if(rose==Jack) -cout<<"OK"<return 0; -}

C: How do you decide to overload an operator with a class member function or a member of a global namespace?
① If an overloaded operator is a class member, the operator is called only if the left operand that is used with him is an object of that class. If the left operand of the operator must be a different type, the operator must be overloaded to a member of the global namespace.
②c++ requires assignment =, subscript [], call (), and member-to-operator must be defined as a class member operator. Any definition of these operators as namespace members will be marked as compile-time errors.
③ If an operand is a class type such as a string class, it is best to define a global namespace member for a symmetric operator such as the Equals operator.


D: 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;

E: Watch out.
(1) The consequences of the operator first to determine whether its return value is an lvalue, or the right value, if the Lvalue is the most return reference, if it is the right value, then return the value directly;

(2) + number such as the operator no object can accommodate the changed value, it is best to return a value for such cases, otherwise you can only create a temporary object in the operator body to accommodate the changed value, if you create a temporary object in the heap returns a pointer or reference, in the operator function outside the body also need to release it, If the returned object is not a reference or pointer, then the efficiency is relatively low. If a numeric value is returned, it is best to add a conversion function to the type's value in the constructor of the class, such as: The return value is of type int, then it is preferable to have an int type as the constructor of the parameter.

(3) in the increment operator, put an integer parameter, which is the post-increment runner, which is the value returned, no formal parameter for the previous increment, and a reference to return, example:

1 classTest2 {3  Public:4Test (x=3) {m_value =x}5Test &operator++();//Pre-increment6Test &operator++(int);//post-increment7 Private:8 Int m_value:9 };TenTest &test::operator++() One { AM_value + +;//Increment First -     return* This;//returns the current object - } theTest test::operator++(int) - { -Test TMP (* This);//Create a temporary object -M_value + +;//re-increment +     returnTemp//returning a temporary object -}

(4) because the cast is for the base data type, the conversion to the class type needs to be customized;

(5) Conversion runner overload declaration form: operator type name (); It has no return type, because the type name represents its return type, so the return type is superfluous.

(6) In general, conversion operators and conversion constructors (that is, constructors with one parameter) are reciprocal, and if there is a constructor test (int), then it is better to have a conversion operator int (). This eliminates the need to provide object parameter overloading operators, such as test A1 (1); Test A2 (2); Test A3; a3 = A1+A2; Do not need to overload the + operator, because for the A1+A2 operation, the system may first find there is no definition for the test of the + operator, if not, it will look for the test class conversion function parameter type of the + operator (because it can be + The type of the result is converted to the test object by a conversion function, because the test class has a parameter of type int and a + operator for the int type, so a1+a2 really executes test (int (A1) + int (A2)), which is Test (3);

(7) For conversion operators, there is also a need to note that if there is a Class A conversion function (constructor) with the B parameter, that B can not have a conversion operator, or there is a conversion of two semantics, such as:

1 class A{a (b&) {...}}; class b{ operator A () {...}};

Then there is a problem with the following statement:

1 b b; A (b); // A (b) has a constructor that can be a, or a conversion operator of B

operator keywords in C + + (overloaded operators)

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.