1. carefully differentiate pointers and references
Pointer can point to any point, and referecnce cannot be changed once determined.
Pointer needs to test whether it is null once, and the reference must be cited.
If you are sure you will point to something and will never change to something else, or you cannot implement it due to the implementation of the syntax, please select reference,
In other cases, try to use pointers.
2. Try to use C ++ for Transformation
Static_cast: complete basic functions
Const_cast: Eliminate Constants
Dynamic_cast: converts a base class pointer to the pointer type of a derived class. If a failure occurs, null is returned. You can also reference the conversion.
Reinterpret_cast: compiler-related, non-portable, bitwise conversion.
New transformation is easier to identify, ugly, easy to detect errors, and persistent to use.
3. Never process arrays in the form of Polymorphism
Never use a base class pointer to delete arrays composed of derived class objects
Base * p = new Derived [10];
Delete [] p;
// This is a big problem. When every object in the array is called, the arithmetic operation of the pointer offsets the length of the Base, instead of the Derived.
4. No default constructor is required
The member functions of the class are too complex. The default value is generally invalid, which involves the determination of valid values.
Follow or do not create, or create is correct.
5. Be alert to [custom type conversion functions]
There are two types of conversion:
Implicit type conversion: overload the operator to convert an object to a required type. (Provide an asDouble method to convert to double instead of operator double for implicit conversion)
Single independent variable structure: an object constructor can be constructed using a single parameter, which can be converted to an object. (Explicit operator or proxy class)
The two represent two directions.
Although type conversion may bring some convenience, it is very dangerous that they may be calling functions you do not intend to call.
The use of proxy classes is a technology that has not yet been seen, and will be analyzed gradually.
6. Differences ++ operator prefix and Postfix Properties
6.1: the return value of the difference function
6.2: note that the post-built architecture is based on the frontend implementation. The example is the best illustration.
6.3: no temporary object is required for the front-end mode.
Class UPInt
{
}
UPInt & UPInt: operator ++ () // frontend type
{
* This + = 1;
Return * this;
}
Const UPInt & UPInt: operator ++ (int) // post-built
{
UPInt oldValue (* this );
++ (* This );
Return oldValue;
}
7. Do not overload & |, Operator
It is difficult for these operators to provide the expected behavior patterns of programmers.
Char * p;
...
If (p & strlen (p)> 10 )...
Such semantics has a specific semantic judgment. When p fails, the subsequent judgment will not be executed, which cannot be provided in the Process of overloading & operators.
, The operator requires the first operation before the comma (,), followed by the second operation. The calculation result is later than the preceding one. However, this mode cannot be provided by the heavy load operation. There is absolutely no guarantee that
Later versions are evaluated earlier. For the overloading of these operators, no matter how hard you work, it is difficult to behave like they should.
8. Understand new and delete with different meanings
8.1 new operator:
String * pstr = new string ("TestString ");
This is usually divided into several steps by the compiler.
Void * memory = operator new (sizeof (string); // operator new allocates memory
Call string: string ("TestString") on * memory; // calls the constructor in memory.
String * pstr = static_cast <string *> (memory); // forcibly converted to string *
8.2 operator new:
Similar to malloc, it is only used to allocate memory. For the allocated memory, you need to call operator delete to release the memory.
8.3 placement new:
Placement new is required to call object constructor on allocated memory to build objects.
New (buffer) string ("TestString ");
Buffer is the memory allocated in some ways in advance. In this case, operator new does not need to allocate the memory, so you can directly return this buffer pointer.
Summary:
If you want to create an object from the heap: using new operator, it not only allocates memory, but also calls the object's constructor
If you only want to allocate memory: Use operator new, the object constructor will not be called.
If you plan to decide the memory distribution mode on the heap object, write your own operator new and use new operator, it will automatically call the operator new
If you want to construct an object on the allocated memory, use placement new