C ++ Primer learning summary chapter 1 operation overloading and type conversion, Chapter 4 primer
Chapter 4 operation overloading and type conversion
1. YesDirect callReload operator functions.
However, if an operator such as the + operator contains both overloaded member functions and overloaded non-member functionsIn this case, if the + number is used directly, an error occurs.Because the compiler does not know which operator to call.
2.Comma,OperatorLogic &&OperatorLogic or |It is not recommended to overload the operator.
Because the preceding three operators have the characteristics of order of evaluation and short-circuit evaluation (& | Features short-circuit Value Calculation), But the overloaded operator is essentially called by the short function, so the order of value and short-circuit value will disappear.
Note that the first a & (v --) printed above is 1 instead of 0. even if you use () to enclose v --, the return value of the expression is still the original value of v.
3.Assign values (=), subscript ([]), call (), and member access arrow (->)Must be defined as a member function of the class (Otherwise, compilation fails.).
4. Reload OperatorThe order of different types of parameters cannot be changed.
Because the + int operator is overloaded, a + v is correct, but v + a is wrong.
But can the following "111" and "s" be changed in any order?
Because the non-member function operator + (string, string) is defined ),Instead of operator + (const char *, string)Therefore, string constants are automatically implicitly converted to string objects.
5. Reload the input and output operators >>and <.
First, the OS is parameter and return value of the two operators must be non-const references, because the stream object cannot be copied and changes the state.
Secondly, for the input operator>, the class object must beNon-const reference. For the output operator <, the class object is recommended to be referenced by const.
6. Equal operator = and unequal Operator! =
Generally, a class defines the = Operator, So we usually need to define a variable! = Operator.
And! = The operator only works by calling the = Operator, that is, only one operator is really responsible for comparison.
7. You can give a classDefine multiple overloaded operators.
8. Overload subscript operator []
Overload subscript operator []First, the reference should be returned.,SecondTwo overload functions, const version and non-const version, are generally defined.
Because B is a constant, we cannot assign values to B [2.
9. Overload increment + + decrease -- operator.
First, we recommend that you reload the prefix and postfix versions for each operator.
Second, the pre-version returns a reference, and the post-version returns a value.
10. Reload the * unreference and-> member access operators.
Note:
* Returns the object reference. The-> access operator returns the address of the member.
-> The fact that the member access operator obtains members never changes.
11. class type conversion
Class type conversion from:Implicit conversion Constructor(Single-parameter constructor) andType conversion OperatorCommon definition.
Implicit conversion Constructor: Convert other types to the type of the current class.
Type conversion Operator: The current class object is converted to another type.
The type conversion operator does not display the return type. It is an intangible parameter and must be defined as a member function of the class.
12. type conversion with Ambiguity
Scenario 1:Suppose we want to convert B type object to A type object now. If A has A constructor whose parameters are B objects and B has A type conversion operator whose target object is A, you can call the conversion constructor of A from B->, you can also call the conversion operator of B to produce ambiguity.
Case 2: If Class A has A conversion with multiple built-in arithmetic types, be careful.
Case 3: Assume that func (Dd) and func (Cc) are two overloaded functions. In addition, both Class D and class C objects can be converted using int. Therefore, the call to func (10) produces ambiguity.
Conclusion: In addition to explicit conversions to the bool type, avoid defining type conversion operators and non-explicit conversion constructors.