vector<T *>的析构问题
定义了一个类,类似于html中的tag解析,每个tag都有parent节点,有一个vector<A *> children保存当前的孩子节点
一共有2个构造函数,如果无参,初始化参数,调用第二构造函数添加孩子节点
第二构造函数接受一个父指针,构造时设置父指针,并递归调用自身添加孩子节点
现在构造出来的对象是ok的,已经尝试读取过文件内容,都是正确的
示例代码:
class A{public: int num; A *parent; vector<A *> children; ... // 无参数调用 A(void) { this->num = 3; this->parent = 0; for (int i=0;i<this->num;++i) this->children.push_back(new A(this,this->num-1)); } // 子构造函数,被上面的无参函数调用 A(A *a,int n) { this->num = n; this->children->parent = a; for (int i=0;i<this->num;++i) this->children.push_back(new A(this,this->num-1)); }};void deallocate(pointer _Ptr, size_type) { // deallocate object at _Ptr, ignore size ::operator delete(_Ptr); }A::~A(void){ if (this->parent!=0) this->parent = 0; for (int i=0;i<(int)this->children.size();++i) this->children[i]->~A(); vector <A *> ().swap(this->children);}