C ++ get started learning (operator overload)

xiaoxiao2021-03-06  14

1. Overload operator 1.1 Restricting the overload operator has the following limitations:

(1) Only operators in the C predefined operator set can be overloaded;

(2) For an operator of the built-in type, its predefined cannot be changed, and should not be built-in type overload operators, such as, cannot change the meaning of INT type operators ;

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

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

(5) The overload operator cannot change their operator priority;

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

(7) In addition to the pair () operators, it is illegal to provide the default streenters of other overload operators;

1.2 Note (1) The consequences of the carrier operator First, determine its return value is the left value, or the right value, if it is the left value, the reference is the reference, and the value is directly returned;

(2) 号, etc. There is no object to accommodate the rear value, and for such a case, it is best to return the value, otherwise it can only be operated to create a temporary object within the consolidation to accommodate the changed value, if it is created in the heap Temporary object returns a pointer or reference, and it needs to be released outside the operator function, if the object returned, not a reference or pointer, then the efficiency is relatively low. If the value is returned, it is best to increase the conversion function of this type value in the constructor of this class, such as the return value is an int type, then it is preferred to have an int type as a configuration function of the parameter.

(3) In the incremental operator, put an integer ginseng, then the incremental operator, it is the value returns, and there is no shape for the former increment, but also returns, examples:

Class test

{

PUBLIC:

TEST (x = 3) {m_value = x}

TEST & OPERATOR (); // Pre-increment

Test & Operator (int); // After increment

Private:

INT M_VALUE:

}

Test & Test :: Operator ()

{

M_Value ; // Premanence

Return * this; // Return to the current object

}

Test Test :: Operator (int)

{

TEST TMP (* this); // Create a temporary object

M_Value ; // re-increment

Return Temp; // Return to Temporary Objects

}

(4) Because forced conversion is for basic data types, the conversion of class types must be customized;

(5) Conversion operator overload declaration form: Operator type name (); it has not returned the type, because the type name represents its return type, so the return type is much more.

(6) In general, the conversion operator and the conversion constructor (ie, a constructor of one parameter) are reversed. If there is a constructor Test (int), then it is preferred to have a conversion operator int (). This doesn't have to provide an object parameter overload operator, such as Test A1 (1); Test A2 (2); Test A3; A3 = A1 A2; no need to overload number operator, because for A1 A2 The operation, the system may find if there is a number operator defined for TEST, if not, it will find a number operator that is not targeted with the Test class conversion function parameter type (because you can run the result of the number " Convert the conversion function to the Test object), because the Test class has an int type parameter, for the int type with operators, the A1 A2 is really executed is TEST (INT (A1) Int (A2)); ie Test (3); (7) For the conversion operator, there is another place to be aware that if there is a conversion function (constructor) with B parameter (constructor) in the Class A, it does not have a conversion operator in the B, otherwise There is a conversion amazing, such as:

Class a {a (b &) {...}}; class b {operator a () {...}}; then the following statement will have problems:

B B; A (b); // A (b) may be a constructor of A, or a conversion operator of B.

转载请注明原文地址:https://www.9cbs.com/read-47355.html

New Post(0)