<Basic topics>
Pointer and reference have two types under the Inheritance Mechanism:
- Static type refers to the type when it is declared;
- Dynamic types are determined by the objects they actually refer.
Clause 1: differentiate pointer and references
No null references. A reference must always represent an object.
You do not need to test its validity before using reference. If pointer is used, it is usually used to test whether it is null.
Differences between pointers and references:
- Pointers can be re-assigned to point to another object;
- The reference always points to the object it initially obtained, and the value of the original object is changed even if the value is re-assigned.
OPERATOR [] returns a reference object that can be used as an assignment value.
Clause 2: it is best to use the C ++ transformation operator
Static_cast: This function is used to forcibly convert data types that do not involve the inheritance mechanism;
Const_cast: used to change the constant or volatileness in the expression );
Dynamic_cast: used to perform a safe downward or cross-series type conversion action in the inheritance mechanism, that is, base objects-> derived objects. If the conversion fails, null pointer (the transformation object is a pointer) or exception (the transformation object is a reference) is returned );
Reinterpret_cast: Convert the user function pointer type.
Clause 3: Never process arrays in the polymorphically mode.
Inheritance: operations on objects that inherit the base class by pointing to the pointer or reference of the base class object.
When declaring an array, apply to the system for memory of numelemt * sizeof (Object Type in the array) size. The system gives the memory of the data size in the object class when it is a famous object. Class member functions are eventually converted to global functions. The memory distance of elements moving one byte in the array is sizeof (array object type ).
Clause 4: it is not necessary to provide default constructor
The lack of default constructor in the classes Class has two problems:
- When an array is generated, the system calls the default constructor;
- Not applicable to many template-based constainer classes.
<Operator>
Article 5: Be alert to custom type conversion functions
Two types of functions allow the compiler to perform implicit conversion: single variable constructor and implicit type conversion operator.Where,Single Variable ConstructorsConstructors that can be successfully called with a single independent variable.Implicit type conversion OperatorIt refers to adding a type name after the keyword operator. You do not need to specify the return value type, because the return value type is basically represented in the function name.
Root problem: such functions may be called without your intention or expectation, and the results may be incorrect and intuitive.ProgramBehavior, difficult to debug.
Solution to the implicit type conversion OPERATOR: Replace the type conversion operator with another function with function equivalence. One of the solutions for a single independent variable constructors is to use the keyword explicit. If you declare constructors as explicit, the compiler cannot call them for implicit type conversion.
Clause 6: differentiate the prefix and Postfix forms of the increment/Decrement Operators
When the increment/decrement operator is overloaded, in order to distinguish between the frontend and the backend, the latter has an int independent variable type, but there is no variable name, such as operator ++ (INT );. When the post-built function is called, the compiler silently specifies a 0 value for the int.
The frontend type and the backend type of the auto-increment and auto-increment operators return different types: The frontend Type Returns a reference, and the latter type returns a const object.
Why does a post-built const object return? (One of the best practices for designing classes is: Once you have any questions, how does the ints behavior comply with it .) To prevent the following actions:
Int I = 4;
I ++; // error, but ++ I is valid
Clause 7: Do not overload &, | and, operators
C ++ uses the so-called true and false expressionsTransient (Short Circuit)The evaluation method means that once the true or false value of the expression is determined, the evaluation is still completed even if some of the expressions are not verified.
Clause 8: understand new and delete with different meanings
String * PS = new string ("Memory Management ");
The new operator used here is built in the language. It always does the following two things. In any case, you cannot change its behavior. Its actions are divided into two aspects:
- It allocates enough memory to place a certain type of objects;
- It calls constructor and sets the initial value for the object in the allocated memory.
Operator New Expression:
New operator calls the operator new function to execute the necessary memory allocation actions. You can reload the function in the class. The operator new function is usually declared:
Void * operator new (size_t size );
Operator new is only responsible for memory allocation, while new operator is responsible for converting the memory returned by operator new into an object.
Expression: string * PS = new string ("Memory Management"); the compiler generates someCode:
Void * Memory = Operator new (sizeof (string); // get raw memory
Call string: string ("memory management") on * Memory // initialize objects
String * PS = static_cast <string *> (memory); // PS point the new object
Placement New Expression:
The placement new expression refers to constructing an object on an existing memory buffer.
For example:
1Widget * constructwidgetinbuffer (Void* Buffer,InWidgetsize)2 {3 4Return New(Buffer) widget (widgetsize );5 6}
This function returns a pointer pointing to a widget object, which is constructed on a memory buffer passed to this function. Therefore, the operator new function of placement new is as follows:
1 Void*Operator New(Size_t,Void*Location)2 {3ReturnLocation;4}
The size_t parameter is not used in the function. The reason why the name is not given is to avoid compiler errors.
Delete and deallocation)
The delete operator of the operator function is the same as the delete operator of the new operator. The memory release action is executed by the operator delete function. It is usually declared as follows:
Void operator Delete (void * memorytobedeallocation );
Therefore,
Delete pS;
After the compiler is converted:
PS-> ~ String (); // call the object's destructor
Operator Delete (PS); // release the memory occupied by the object
However, it should be noted that placement new builds objects from existing memory areas. Therefore, operator Delete cannot be called directly to release the memory applied for by the function. Therefore, call the destructor to parse the object and return the memory.
Array new and delete
When allocating memory to arrays, new operator calls operator new [], while Delete operator calls operator Delete [].