Clause 5: Be careful with user-defined conversion functions
C ++ allows the compiler to implicitly convert two data types. (Implicit conversion). First, C ++ inherits the type conversion policy of C language.
In addition, C ++ has two types of implicit conversions: constructor for a single parameter and implicit type conversion operators. The constructor of a single parameter refers to passing only to it
A constructor that can be called by one parameter. This constructor may define only one parameter or multiple parameters. When defining multiple parameters, the first one is required.
All parameters following the parameter have a default value. The following are two examples:
Class Name {
Public:
Name (const string & S );
....
};
Class rational {
Public:
Rational (INT number = 0, int denominator );
....
};
Implicit type conversion characters are generally defined as follows:
Class
{
...
Operator B ()
{
...
Return B;
}
};
This defines the conversion operators from A to B.
Clause 6: differentiate the prefix and suffix form of the auto-increment and auto-increment operators.
I ++ and I are quite familiar to everyone. We know that C ++ can overload operators. Similarly, we can also define custom addition and subtraction operators for classes. To distinguish between prefix auto-increment and suffix auto-increment, C ++ specifies that the suffix auto-increment form has an int type parameter, which is equivalent to a placeholder and has no practical significance.
Example:
Class upnint {
Public:
Upnint & operator ++ (); // prefix auto-Increment
Const upnint operator ++ (INT); // auto-incrementing suffix
Upnint & operator -- (); // prefix auto-Subtraction
Const upnint operator -- (INT); // suffix auto-Subtraction
Upnint & operator + = (const upnint & UPI );
};
Clause 7: Do not reload "&", "|" and ","
Like C, C ++ uses the short-circuit evaluate method to evaluate a Boolean expression. Once a Boolean expression is determined to be true or false, the Boolean expression stops operations even if some expressions are not tested. For example:
Char * P;
...
If (P! = 0) & (strlen (p)> 10) do not worry about calling strlen when P is empty. If P is not equal to 0, the test fails. Strlen will never be called. C ++ allows customization of user-defined types & | operators. The method is to reload the operator & function and operator | function. This type of overload can be global or targeted at a class. However, if you decide to adopt this method, you must know that it will bring fundamental changes to game rules. Because you replace the short-circuit evaluate method with the semantics of function call. That is to say, if you overload the & operator. The following code, if (expression1 & expression2)... for the compiler, is similar:
If (expression1.operator & (expression2) // when operator is a member function
If (expression1.operator & (expression2) // when operator is global function
This does not seem quite different. However, the semantics of function calls and short-circuit evaluation are absolutely different in the following two aspects. First, when calling a function, evaluate all parameters. Therefore, when operator & and operator | are called, both parameters need to be calculated. In other words, it does not adopt the short-circuit evaluation method. Second, the C ++ language specification does not define the order in which function parameters are evaluated, so there is no way to know which one of expression1 and expression2 is evaluated first. This is very unnecessary with the short-circuit evaluation method. The short-circuit evaluation method always evaluates the parameters from left to right.
Therefore, if you reload & |, there is no way to provide programmers with the behavior characteristics they expect and are used. So do not reload & and |.
Similarly, the comma operator also has this problem. Generally, you do not need to reload them, do you ,?