"New" is a keyword in C + + that is typically used to create an object, but unlike the general method of creating an object, it returns a pointer to the object. When we use the keyword new to create an object dynamically on the heap, it actually does three things: get a chunk of memory, call the constructor, and return the correct pointer.
As we have defined the following class A:
Class A
{
int i;
Public:
A (int _i): I (_i*_i) {}
void Say () {printf ("i=%d/n", I);}
};
Call NEW:
* pa = new A (3);
So the process of creating an object above is roughly equivalent to the following three sentences (in general): a
* pa = (*) malloc (sizeof (A) *3);
PA->A::A (3);
Return PA;
The point is to understand that when new creates an object it returns the pointer to the image, returns the pointer to the image, and returns the pointer to the image. Important thing to say three times.