9, Just as positioningNew (placement new expression)Expression is usedAllocatorClassConstructLow-level selection of members,You can call the Destructor explicitly.DestroyLow-level selection of functions.
ExampleCode
For (T * P = first_free; P! = Elements;/* empty */) alloc. Destroy (-- p); // The upper expression is equivalent to the lower expression ~ T ();
10Explicitly calling the Destructor is to clear the object itself, and does not release the memory occupied by the object; callingOperator DeleteThe function does not run the destructor. It only releases the specified memory.
11Class by defining its own nameOperator newAndOperator DeleteCan be used to manage memory of its own type.
The compiler sees the class type ( For a class type ) New or Delete , it checks whether the class has operator new or operator Delete member, if the class defines (or inherits) its own member New and Delete function, the functions are used to allocate and release memory for the object; otherwise, the standard library version is used.
OptimizationNewAndDeleteYou only need to defineOperator newAndOperator DeleteNew version,NewAndDeleteThe expression removes the construction of a self-care object.
12, In the customNewAndDeleteIn pairs. Use all the custom and global.
You can use the global scope to determine the operator to forceNew,DeleteThe expression uses a global library function.
Sample Code
Type * P =: new type; // uses global operator New: Delete P; // uses global operator Delete
13If the base class hasVirtualThe Destructor is passedOperator DeleteThe size varies according to the dynamic type of the object indicated by the deleted pointer.VirtualDestructor, then, the behavior of deleting the pointer pointing to the object of the derived class through the base class pointer is as undefined as usual.
These functions (Operator new And Operator Delete ) Is implicitly a static function and does not need to be explicitly declared Static . Member New And Delete Functions must be static, because they must be used before the object is constructed ( Operator new ), Or use ( Operator Delete ). Therefore, these functions have no member data to manipulate. Like any other static member function,NewAndDeleteYou can only directly access static members of the class.
14 A general policy is to pre-allocate an original memory to save unconstructed objects. when creating new elements, they can be constructed in a pre-allocated object; when releasing elements, place them back in the pre-allocated object block instead of returning the memory to the system. This policy is often called to maintain a free list. The Free List can be implemented as a linked list of allocated but unconstructed objects.