More Effective C ++ Terms 21

zhaozj2021-02-08  462

Terms 21: Avoid implicit type conversion by overloading

The following is a code, if there is no unusual reason, I can't see anything:

Class Upint {// unlimited precision

Public: // integers class

UPINT ();

Upint (int value);

...

}

/ / Related to why return value is Const explanation, see Effective C Terms 21

Const Upint Operator (Const Upint & lhs);

UPINT UPI1, UPI2;

...

UPINT UPI3 = UPI1 UPI2;

I can't see any surprising thing here. All UPI1 and UPI2 are UPINT objects, so they add even the UPINTS's Operator function.

Now consider these statements:

UPI3 = UPI1 10;

Upi3 = 10 UPI2;

These statements can also run successfully. The method is to convert the shaping number 10 to UPINTS by establishing a temporary object (see Terms 19).

Let the compiler do this type of conversion is really very convenient, but the establishment of a temporary object is overhead, and we don't want to bear this overhead. Just like most people just want to benefit from the government without wanting this, most C programmers want to carry out implicit type conversions without temporary object overhead. However, in the field of calculation, the deficit is can't, how can we do this?

Let us fall back, realize that our purpose is not really a type conversion, but use UPINT and INT as a parameter to call Operator. Implicit type conversion is just means to achieve the purpose, but we don't confuse the means and purpose. There is also a method to successfully perform the hybrid type call of Operator, which will eliminate the need for implicit type conversion. If we want to add upint and int objects, we have achieved this purpose by declaring the following functions, each function has a different parameter type set.

Const Upint Operator (Const Upint & lhs, // Add Upint

Const Upint & rhs); // and upint

Const Upint Operator (Const Upint & lhs, // Add Upint

INT rHS); // and Int

Const Upint Operator (int LHS, / / ​​Add Int and

Const Upint & rhs; // Upint

UPINT UPI1, UPI2;

...

UPINT UPI3 = UPI1 UPI2; / / correct, no by UPI1 or UPI2

// Generated temporary object

Upi3 = Upi1 10; // correct, no by Upi1 or 10

// Generated temporary object

UPI3 = 10 UPI2; // correct, no 10 or UPI2

// Generate temporary objects.

Once you start using the function overload to eliminate type conversion, you may be able to declare the function and put yourself in danger:

Const Upint Operator (int LHS, INT RHS); // Error! This idea is reasonable. For UPINT and INT types, we want to overload the Operator function with all possible combinations. Only three overload functions are given, and the only leaks are Operator with two int parameters, so we want to put it.

Is it reasonable? One rule in C is that each overloaded Operator must have a user-defined type. INT is not a user-defined type, so we cannot overload Operator to become a function with this type of parameters. (If there is no rule, the programmer will change the predefined operation, so it will definitely introduce the program into the chaos. For example, it will change the above-mentioned Operator, will change the meaning of the INT type.)

The method of avoiding temporary objects is not just in the Operator function. For example, in most programs, you want to allow places where you can use String objects, or use char *, and vice versa. Also if you are using the numeric (digital) class, such as Complex (see Terms 35), you want to make int and double types such as anywhere in the Numeric object. Therefore, any function with string, char *, and Complex parameters can be used to eliminate type conversions.

However, you must keep the 80-20 rules (see Terms 16). There is no need to achieve a large number of overload functions, unless you have reason to be aware that the overall efficiency will have significantly improved after the program uses the overload function.

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

New Post(0)