More Effective C ++ Terms 6

zhaozj2021-02-08  425

Terms 6: Self-incrementation, Self-reduction (Decrement) operator prefix form and suffix form

A long time (eighty in the 1980s), there is no way to distinguish between and - operators' prefixes and suffix calls. This problem was complained by the programmer, so the C language has been extended, allowing two forms of the Increment and Decrement operators to be overloaded.

However, there is a problem in an syntactic, and the difference between the overload function is determined in their parameter type, but whether it is an increment or Decrement's prefix or a suffix has only one parameter. In order to solve this language problem, the C specified suffix form has an int type parameter. When the function is called, the compiler passes a 0 value for the INT parameter to the function:

Class Upint {// "unlimited precision int"

PUBLIC:

UPINT & OPERATOR (); // prefix

Const Upint Operator (int); // suffix

UPINT & OPERATOR - (); // - Prefix

Const Upint Operator - (int); // - suffix

UPINT & OPERATOR = (int); // = operator, Upints

/ / With the INTS phase

...

}

UPINT I;

i; // call I.operator ();

i ; // call I.operator (0);

--i; // call I.operator - ();

I -; // Call I.Operator - (0);

This specification has some weird, but you will habits. It is especially noted that these operator prefixes are different from the returning value of the suffix form. The prefix returns a reference, the suffix forms returns a const type. Below we will discuss the prefix form of operators and suffixes, which are also used as-operators.

Starting from the date you started to make C programmers, you remember that increment is sometimes called "Add and then Retrieve", the suffix form is called "Retrieve and then increase". These two sentences are very important because they are specifications in the Increment prefix and suffix.

// Prefix form: increase then take the value

UPINT & UPINT :: Operator ()

{

* this = 1; // increase

Return * this; // Take the value

}

// Postfix Form: fetch and increment

Const Upint Upint :: Operator (INT)

{

UPINT OLDVALUE = * this; // Remove the value

(* this); // increase

Return OldValue; // Returns the retrieved value}

The suffix operator function does not use its parameters. Its parameters are just used to distinguish prefixes and suffix functions. If you don't use parameters in a function, many compilers will display warning messages, which is very annoying. In order to avoid these warning messages, the number of parameters you don't want to use is omitted when using the method of use; as shown above.

Obviously a suffix incrtain must return an object (it returns the value before adding), but why is a const object? Suppose is not a const object, the following code is correct:

UPINT I;

i ; // Two increment suffixes

// operation

This set of code is the same as the code below:

I.Operator (0) .Operator (0);

Obviously, the object returned by the first calling Operator function calls the second Operator function.

There are two reasons leading us that we should disgust the above practice, and the first is inconsistent with the built-in type behavior. When designing a class encountered a problem, a good criterion is the same as the behavior of the class consistent with the int type. The int type does not allow continuous two suffix increment:

INT I;

i ; // error!

The second reason is the inconsistency between the results generated by the two suffix increment and the caller expectation. As shown above, the value of the second call Operator change is the value of the returned object in the first time, not the value of the original object. So if:

i ;

It is legal, i will only increase it. This is contrary to people's intuition, making people confused (the same is true for int types and UPINT), so it is best to ban it.

C prohibits the int type, and you must also prohibit you from writing the class. The easiest way is to let the suffix increment returns a const object. When the compiler encounters this code:

i ; // Same as I.operator (0) .operator (0);

It finds that the Const object returned from the first Operator function calls the Operator function, however this function is a non-const member function, so the Const object cannot call this function. If you think about letting a function returns a const object without any meaning, now you know sometimes useful, the suffix increment and Decrement are examples. (More examples see Effective C Terms 21)

If you care about the efficiency problem, you may feel some questions when you first see the suffix increment function. This function must establish a temporary object to be used as its return value, (see Terms 19), the above implementation code establishes a displayed temporary object (OldValue), which must be constructed and last structured. The prefix the increment function does not have such a temporary object. This is thus a surprising conclusion, and if only code efficiency is to improve the code efficiency, the Upint caller should try to use the prefix increment, less suffix increment, unless you really need to use the suffix increment. Let us clearly use the prefix increment as much as possible when dealing with the type of user defined because it is high. Let's observe the suffix with the prefix increment operator. They are the same in addition to the return value, which is the same, that is, the value is added. In short, they are considered to be functional. So how do you make sure the suffix increment and prefix increment? When different programmers go to maintain and upgrade the code, what can they ensure that they do not have difference? This can be ensured unless you abide by the principles in the above code. This principle is that the suffix increment and Decrement should be implemented according to their prefix form. You just need to maintain the prefix version because the suffix form is automatically consistent with the behavior of the prefix.

As you can see, master the prefix and the suffix increment and Decrement are easy. Once you understand the rules that their correct return value and the suffix operator should be implemented based on the prefix operator, it is enough.

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

New Post(0)