C ++ Operator Overloading
The operation objects of pre-defined operators in C ++ can only be basic data types. However, similar operations are also required for many user-defined types (such as classes. In this case, you must redefine these operators in C ++ and assign new functions to existing operators so that they can be used for specific operations of specific types. The essence of Operator Overloading is function overloading, which provides the scalability of C ++. Operator Overloading is implemented by creating operator functions, which define the operations to be performed by overloaded operators. The definition of an operator function is similar to that of other functions. The only difference is that the name of an operator function is composed of the operator keyword and the operator symbol to be overloaded. The general format of the operator function definition is as follows: <return type specifier> operator <operator symbol> (<parameter table>) {<function body >}( 1) Except for the class relational operator ". ", member pointer operator ". * ", scope operator": ", sizeof operator and three-object operator "?: ", All operators in C ++ can be overloaded. (2) Heavy-duty operators are restricted to operators that allow heavy-duty within the range of existing operators in the C ++ language. New operators cannot be created. (3) Operator Overloading is essentially a function overload. Therefore, the compiler selects Operator Overloading Based on the function overloading principle. (4) The user-defined class operators must be reloaded before they can be used. However, with two exceptions, the operators "=" and "&" do not have to be reloaded. Which operators can be used as reloads? Almost all operators can be used as overload. Include: Arithmetic Operator: =,-, *,/, %, ++, -- bitwise OPERATOR:-, | ,~, ^, <, >>; Logical operator :! &, |; Comparison operator >,<,>=, <=, == ,! = Value assignment operator: =, + =,-=, * =, % =, \=^ =, <=, >>=; other operators: [], () ->, ', new, delete, new [], delete [],-> *. there are two types of operators: member function form and friend function form. Both types can be private members in the category. Example: A custom int package class that encapsulates the int and supports the + operator and the = Operator 1. use a member function to overload the operator class Integer {public: Integer (); Integer (int value); Integer operator + (int value); void operator = (int value); private: int m_value;}; // integer. cpp # include "integer. h "Integer: Integer () {m_value = 0;} Integer: Integer (int value) {m_value = value;} Integer: operator + (int value) {int tmpValue = m_value + value; return Integer (tmpValue);} vo Id Integer: operator = (int value) {m_value = value;} example: Integer integer = Integer (10); Integer tmpInteger = 100; // overload = Operator tmpInteger = integer + 1; // overload + operator 2. using youyuan function to overload operators using member function overload operators, we implement integer + 1. But if it is 1 + integer, we must use youyuan function overload + operator at this time: // integer. hclass Integer {public: Integer (); Integer (int value); Integer operator + (int value); void operator = (int value); operator int () const; private: I Nt m_value; friend Integer operator + (int value, Integer integer) ;}; // Integer operator + (Integer integer, int value); // This function cannot be declared, otherwise, it will conflict with the member Function Integer operator + (int value, Integer integer); // integer. cpp # include "integer. h "Integer: Integer () {m_value = 0;} Integer: Integer (int value) {m_value = value;} Integer: operator + (int value) {int tmpValue = m_value + value; return Integer (tmpValue );} Void Integer: operator = (int value) {m_value = value;} Integer: operator int () const {return m_value;} Integer operator + (int value, Integer integer) {int tmpValue = integer. m_value + value; return Integer (tmpValue);} example: Integer integer = Integer (10); Integer tmpInteger = 100; // reload = Operator tmpInteger = integer + 1; // reload the Integer member function + operator tmpInteger = 1 + tmpInteger; // reload the Union function + operator conversion operator overload: From the above code, we can see the Integ Er integer = 1; (it is really convenient to call the constructor implicitly), but if you want to implement int I = Integer (100 ), to convert a custom type to an internal type or to an existing type, you must use the conversion operator. The form of the conversion operator is as follows: X: operator T () where T is the type. The sample code is as follows: // integer. hclass Integer {public: Integer (); Integer (int value); Integer operator + (int value); void operator = (int value); operator int () const; // int conversion operator private: int m_value; friend Integer operator + (int value, Integer integer) ;}; // Integer operator + (Integer integer, int value ); // This function cannot be declared; otherwise, it will conflict with the member Function Integer operator + (int value, Integer integer); // integer. cpp # include "integer. h "# incl Ude <stdio. h> # include <stdlib. h> Integer: Integer () {m_value = 0;} Integer: Integer (int value) {m_value = value;} Integer: operator + (int value) {int tmpValue = m_value + value; return Integer (tmpValue);} void Integer: operator = (int value) {m_value = value;} Integer: operator int () const {return m_value;} Integer operator + (int value, Integer integer) {int tmpValue = integer. m_value + val Ue; return Integer (tmpValue);} example: Integer integer = Integer (10); Integer tmpInteger = 100; // reload = Operator tmpInteger = integer + 1; // reload the Integer member function + operator tmpInteger = 1 + tmpInteger; // reload the friend functions + operator int num = tmpInteger; // reload the int conversion operator and the plus OPERATOR: two prefixes are used for each suffix operation. to distinguish these two operations, the suffix operator is treated as a binary operator. The sample code is as follows: // integer. hclass Integer {public: Integer (); Integer (int value); Integer operator + (int value); void operator = (int value); Comment Ator int () const; // int conversion operator Integer operator ++ (); // overload ++ Integer operator ++ (int value ); // reload Integer ++ private: int m_value; friend Integer operator + (int value, Integer integer) ;}; // Integer operator + (Integer integer, int value ); // This function cannot be declared; otherwise, it will conflict with the member Function Integer operator + (int value, Integer integer); // integer. cpp # include "integer. h "Integer: Integer () {m_value = 0;} Integer: Integer (int va Lue) {m_value = value;} Integer: operator + (int value) {int tmpValue = m_value + value; return Integer (tmpValue);} void Integer :: operator = (int value) {m_value = value;} Integer: operator int () const {return m_value;} Integer: operator ++ () {Integer tmpInteger; tmpInteger = ++ m_value; return tmpInteger;} Integer: operator ++ (int value) {Integer tmpInteger; tmpInteger = m_va Lue ++; return tmpInteger;} Integer operator + (int value, Integer integer) {int tmpValue = integer. m_value + value; return Integer (tmpValue);} example: Integer integer = Integer (10); Integer tmpInteger = 100; // reload = Operator tmpInteger = integer + 1; // reload the Integer member function + operator tmpInteger = 1 + tmpInteger; // reload the friend functions + operator int num = tmpInteger; // reload the int conversion operator tmpInteger = integer ++; // reload Integer ++ tmpInteger = ++ integer; // Compare the methods of overload ++ Integer: In most cases, it is possible to overload operators as member functions of the class and friend functions of the class. However, member function operators and friend function operators also have their own characteristics: (1) In general, it is best to reload a single object operator as a member function of the class; for binary operators, it is best to reload the functions as friend functions of the class. (2) The following binary operators cannot be overloaded as class friend functions: =, (), [],->. (3) A type conversion function can only be defined as a member function of a class, rather than a friend function of a class. (4) If an operator needs to modify the object state, it is better to select reload as a member function. (5) If the required operands (especially the first operand) of the operator want to have implicit type conversion, you can only use the youyuan function. (6) When an operator function is a member function, the leftmost operand (or only the leftmost operand) it must be a Class Object of the operator class (or a reference to this class object ). If the operand on the left must be an object of different classes or an internal type, this operator function must be implemented as a friend function. (7) When the overload operator is required to be interchangeable, select the overload as a friend function.