我将矢量定义为类Grid的私有变量。 Class Points只有两个实例变量都是整数,但只有当我从文件中读取这些变量时才知道点的数量,所以我想我必须动态地用新变量来表示它,这意味着我必须在以后销毁它们。我是否正确地初始化构造函数,并且在为Grid编写析构函数时,是否需要为这样的向量编写析构函数:〜vecotr()或者使用delete或使用迭代器?我需要删除矢量吗?
class Grid{
public:
// initialize vector with 3 points with val 0.
Grid(vector<Points> v) : vector<Points>(3, 0) {}; // is this right
// first option
~Grid() {
~vector<Points>(); // not sure how to destroy vector<Points>;
}
// second option
~Grid() {
delete v_points;
}
// third option
~Grid() {
for (vector<Points>::iterator it = v_points.begin(),
vector<Points>::iterator it_end = v_points.end(); it != it_end; it++)
}
private:
vector<Points> v_points;
};
我应该使用哪个选项,并且是否正确初始化构造函数?
@Anycom嗯,我会打电话给新的积分来创建每个新点,然后我使用第三个选项? – Napalidon
@Napalidon,是否有一个特定的原因,而不是没有'新'的做? – chris
@Napalidon我不认为你想使用新的填充点,除非点是一个指针(我的预感不是)。 – Anycorn