new和delete
Initializing Dynamically Allocated Objects
We indicate that we want to value-initialize the newly allocated object by following the type name by a pair of empty parentheses. In the case of class types (such as string ) that define their own constructors, requesting value-initialization is of no consequence: The object is initialized by running the default constructor whether we leave it apparently uninitialized or ask for value-initialization. In the case of built-in types or types that do not define any constructors,the difference is significant:
int *pi = new int; // pi points to an uninitialized int int *pi = new int(); // pi points to an int value-initialized to 0
int *pi = new int(1024); // object to which pi points is 1024
string *ps = new string(); // initialized to empty string string *ps = new string; // initialized to empty string
It is legal to delete a pointer whose value is zero; doing so has no effect. Setting the pointer to 0 after the object it refers to has been deleted makes it clear that the pointer points to no object.
浙公网安备 33010602011771号