简体   繁体   English

我需要删除载体吗?

[英]Do I need to delete a vector?

I am defining a vector as a private variable of a class Grid. 我将向量定义为Grid类的私有变量。 Class Points has only two instance vars that are all ints but the number of points will only be known when I read this in from the file, so I guess I have to make Points dynamically with new which means I have to destroy them later. 类积分只有两个实例变量,它们都是整数,但是只有从文件中读取积分时才知道积分的数量,所以我想我必须用新的动态制作积分,这意味着以后必须销毁它们。 Did I initialize constructor correctly and When writing a destructor for Grid do I need to write a destructor for vector like this: ~vecotr() or with delete or using iterator? 我是否正确初始化了构造函数,并且在为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;
};

Which option should I use and did I initialize constructor correctly? 我应该使用哪个选项?是否正确初始化了构造函数?

If object is not allocate with new , you do not need to explicitly destroy it. 如果没有使用new分配对象,则无需显式销毁它。 The member objects will be automatically destroyed in the reverse order of their declaration. 成员对象将按照其声明的相反顺序自动销毁。 In your case there is no need to even create a destructor as the automatic one will suffice. 在您的情况下,甚至不需要创建析构函数,因为自动析构函数就足够了。

If for some reason you do have member objects allocated with new, you also have to create custom copy construct and assignment operator otherwise you run into troubles of sharing the same member object across multiple instances. 如果由于某种原因确实为成员对象分配了new,则还必须创建自定义副本构造和赋值运算符,否则会遇到在多个实例之间共享同一成员对象的麻烦。

I will reply because predecessors answered only to the question from topic while you are asking more questions and I will also give some advice. 我会回答,因为在您提出更多问题时,前任仅回答了主题中的问题,我也会提供一些建议。 In the remainder of this post we will assume that if Points do allocate any dynamic memory, the memory is returned properly when Points are deleted. 在本文的其余部分中,我们将假定如果Points确实分配了任何动态内存,则删除Points会正确返回该内存。

Class Points has only two instance vars that are all ints but the number of points will only be known when I read this in from the file, so I guess I have to make Points dynamically with new which means I have to destroy them later. 类积分只有两个实例变量,它们都是整数,但是只有从文件中读取积分时才知道积分的数量,所以我想我必须用新的动态制作积分,这意味着以后必须销毁它们。

This comes with contradiction to what you actually do here 这与您实际上在这里所做的矛盾

class Grid{
public:
  // initialize vector with 3 points with val 0.
  Grid(vector<Points> v) : vector<Points>(3, 0) {};  // is this right

private:
  vector<Points> v_points;
};

because you create vector without new . 因为您创建的向量没有new However this might be OK if we assume that you first get number of Points and then you are about to create a Grid. 但是,如果我们假设您首先获得Points ,然后您将要创建网格,则这可能是可以的。 std::vector is not C array and might be easily resized, assigned and gives much more flexibility. std::vector不是C数组,可以轻松调整大小,分配并提供更大的灵活性。 You do not have to create it on heap because you are afraid about size: vector elements are always created on heap, it is just a vector itself that is (if it is) on stack. 您不必在堆上创建它,因为您担心大小:向量元素总是在堆上创建,它只是一个向量本身(如果存在)在堆栈上。 And this is often exactly what we want (see ie RAII ). 这通常正是我们想要的(请参见RAII )。 The proper way to initialize a vector would be then 然后初始化向量的正确方法是

class Grid{
    public:
      // initialize vector with 3 Points with val Points(0)
      Grid(vector<Points> v) : v_points(3, Points(0)) {};

    private:
      vector<Points> v_points;
};

Note, we do it in initialization list. 注意,我们在初始化列表中进行操作。 For POD class members, it makes no difference, it's just a matter of style. 对于POD类成员,这没有什么区别,这只是样式问题。 For class members which are classes, then it avoids an unnecessary call to a default constructor. 对于属于类的类成员,则可以避免不必要地调用默认构造函数。

Did I initialize constructor correctly and When writing a destructor for Grid do I need to write a destructor for vector like this: ~vecotr() or with delete or using iterator? 我是否正确初始化了构造函数,并且在为Grid编写析构函数时是否需要为向量编写析构函数,例如:〜vecotr()或使用delete或使用迭代器?

You don't initialize constructor, rather in constructor initializer list you initialize class members. 您不初始化构造函数,而是在构造函数初始化列表中初始化类成员。 Since vector is not allocated with new , you will not delete it (call delete ). 由于vector没有分配给new ,因此您将不会delete它(调用delete )。 It will be destroyed automatically when Grid is destroyed. 销毁Grid时,它将自动销毁。

// fourth (and correct) option:
~Grid() {
}

Or just leave out the destructor entirely; 或者只是完全销毁析构函数; the compiler-generated destructor will do the right thing here. 编译器生成的析构函数将在此处执行正确的操作。

If you create an object with new you must delete it. 如果使用new创建对象,则必须将其删除。 If you don't, you mustn't. 如果不这样做,就一定不要。

I am assuming that Points does not have any dynamically allocated memory. 我假设Points没有任何动态分配的内存。 If this is the case then all you need is 如果是这种情况,那么您所需要做的就是

~Grid() { }

If it does then you need to delete that dynamically allocated memory for each item in the vector (or use smart pointers). 如果是这样,则需要删除向量中每个项目的动态分配内存(或使用智能指针)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM