Set array of object to null in C++
问题 Suppose I have an array of objects of type Foo in C++: Foo array[10]; In Java, I can set an object in this array to null simply by: array[0] = null //the first one How can I do this in C++? 回答1: Use pointers instead: Foo *array[10]; // Dynamically allocate the memory for the element in `array[0]` array[0] = new Foo(); array[1] = new Foo(); ... // Make sure you free the memory before setting // the array element to point to null delete array[1]; delete array[0]; // Set the pointer in `array[0]