水果类:
#include <string>
using namespace std;
class Fruits {
string color;
public:
Fruits() {
color = "r";
}
};
主要内容:
#include "Fruits.cpp"
void main() {
Fruits* fruits[6];
fruits[0] = new Fruits();
// delete[] fruits[0]; // <-- does not work (deletes one object)
// delete[] fruits; // <-- does not work (deletes all objects in the array)
}
我该怎么做?
最佳答案
delete fruits[0]
将为您删除那个对象。 delete[]
用于删除该数组的所有非空元素,但 fruits[0]
不是对象数组。
关于c++ - C++中删除数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33847783/