【问题标题】:vector <T *> destructor [closed]矢量 <T *> 析构函数 [关闭]
【发布时间】:2012-06-01 15:23:23
【问题描述】:

我有一个类定义如下:

Class A
{
public:
    int num;
    A *parent;
    vector<A *> children;
    ...

    // constructor without parameters
    A(void)
    {
        this->num = 3;
        this->parent = 0;
        for (int i=0;i<num;++i)
            children.push_back(new A(this,num-1));
    }

    // constructor with parameters
    A(A *a,int n)
    {
        this->num = n;
        this->children->parent = a;
        for (int i=0;i<num;++i)
            this->children.push_back(new A(this,this->num-1));
    }
};

现在,构造函数工作正常。析构函数有一些问题。 目前,析构函数定义为:

A::~A(void)
{
    if (this->parent!=0) this->parent = 0;
    for (int i=0;i<(int)children.size();++i)
        this->children[i]->~A();
    vector <A *> ().swap(this->children);
}

但每次调试时,它都会在以下位置中断:

void deallocate(pointer _Ptr, size_type)
    {    // deallocate object at _Ptr, ignore size
    ::operator delete(_Ptr);
    }

看起来我无法删除 this->children 向量中的指针,有什么方法可以成功解构类吗?

【问题讨论】:

  • 旁白:为什么要将children.size() 转换为int,而您可以将循环变量声明为size_t
  • 如果您使用vector&lt;shared_ptr&lt;A&gt; &gt;,您将不需要任何代码并且您的程序将更加健壮。这是一个选择吗?
  • 它叫做destructor,而不是deconstructor
  • 很确定这条线不会编译:this-&gt;children-&gt;parent = a;
  • @larsmans:根据您使用的 STL 实现,vector::size_type 可能没有使用默认分配器的size_type,但可能直接使用size_t,或完全使用其他东西。

标签: c++ memory-management vector


【解决方案1】:

你的析构函数应该是:

A::~A(void)
{
    for (size_t i=0; i < children.size(); ++i)
        delete children[i];
}

您可能还应该研究复制构造函数。否则,这样的代码会失败:

{
    A foo;
    B bar = foo;
}

因为你会删除相同的指针两次。

这两个帖子中的一个可以帮助您理解:12

【讨论】:

    【解决方案2】:

    在 A 的析构函数中,你不应该显式调用子项的析构函数,而只需删除向量的元素。这应该足以满足您的目的:

    A::~A(void)
    {
        for (size_t i=0; i<children.size(); ++i)delete children[i];
    }
    

    【讨论】:

      【解决方案3】:

      您的带参数的构造函数中存在拼写错误 - this-&gt;children-&gt;parent 不会编译,因为 children 不是指针,vector 也没有 parent 成员。我怀疑你打算改用this-&gt;parent

      您的析构函数直接调用每个子对象的析构函数,这将销毁子对象但不会释放其内存。您使用new 运算符分配子对象,因此您需要使用delete 运算符来调用析构函数并正确释放内存。

      附带说明,您的num 成员是多余的。您可以在需要num 的任何地方使用children.size()

      试试这个:

      class A 
      { 
      private:
        static const int default_num = 3;
      
      public: 
          A *parent; 
          vector<A *> children; 
          ... 
      
          // default constructor
          A() 
          { 
              parent = 0; 
              for (int i = 0; i < default_num; ++i) 
                  children.push_back(new A(this, default_num-1)); 
          } 
      
          // constructor with parameters 
          A(A *a, int n) 
          { 
              parent = a; 
              for (int i = 0; i < n; ++i) 
                  children.push_back(new A(this, n-1)); 
          } 
      
          ~A()  
          {  
              parent = 0;  
      
              for (vector<A *>::size_type i = 0; i < children.size(); ++i)  
                  delete children[i];  
              /*
              alternatively:
              for (vector<A *>::iterator i = children.begin(); i != children.end(); ++i)  
                  delete *i;  
              */
      
              children.clear(); // optional, will be handled when vector is implicitally destructed
          }  
      };
      

      【讨论】:

        猜你喜欢
        • 2022-11-20
        • 2020-06-20
        • 1970-01-01
        • 2020-05-18
        • 1970-01-01
        • 1970-01-01
        • 2017-09-03
        • 2017-07-27
        • 1970-01-01
        相关资源
        最近更新 更多