In C ++, new actually has three meanings:
1. New operator
2. Operator new
3. Placement new
Our most common usage is
A * A = new ();
Here, new is the new operator, which consists of three steps:
1. allocate memory
2. Call the constructor of.
3. Return memory pointer
The first step is to allocate memory, which is actually to use operator new. The effect is similar to that of malloc. If you call it directly, it is as follows:
A = Operator new (size); or
Operator new [] (size );
Step 2: Call the object constructor. The actual call is placement new. If you call the constructor directly, it is as follows:
New (a) ();
If you use placement new (that is, a is the pointer on the stack) for the pointer on the stack, you must explicitly call the destructor.
A-> ~ A (); this is the only situation where you need to explicitly call the destructor.