02年世界杯韩国黑哨_曲棍球世界杯 - guanchafang.com

指向对象的指针的正确销毁方法
2026-03-03 06:07:00

我想问一些关于int指针和vector指针的正确销毁方式的问题。首先,我曾经看到人们询问这种类型的问题,几乎总会有几个回答说,在C++中使用向量指针、对象指针等不是好的标准C++编码实践,而且你应该实例化对象的一个副本。这可能是正确的,但你并不总是能够控制在你到来之前已经形成的范式。我所需遵循的范式要求几乎将所有东西都初始化为指针,这类似于Java对C++的处理方式。我们这样做的主要原因是我们的数据集非常大,堆栈分配可能会导致溢出。

我的问题:

如果我有一个int32_t数组的指针,什么是在析构函数中销毁它的正确方法?

注意: 我们的惯例是在构造函数中将任何指针设置为NULL。

I initialize it as a member variable.

int32_t *myArray_;

When I use it in a method, I would:

this->myArray = new int32_t[50];

To delete it in the method I would call delete on the array:

delete [] this->myArray;

What is the proper call in the destructor?

~MyDestructor(){

delete this->myArray_;

or delete [] this->myArray_;

}

关于向量指针,我有同样的问题:

I initialize it as a member variable.

std::vector *myVector_;

When I use it in a method, I would:

this->myVector_ = new std::vector();

//pushback some objects

To delete the vector in the method I would iterate the vector and delete its objects, then delete the vector;

for(std::vector::iterator itr = this->myVector_->begin();

its != this->myVector->end(); ++ itr){

delete (*itr);

}

delete this->myVector_;

What would be the proper way to clean it up in the destructor?

would I just delete the vector?

delete this->myVector;

or do I need to iterate the entire vector again?

非常感谢您提前给予的任何建议。

有哪些冷色调的颜色很经典漂亮
舒适到上头!这10家新兴内衣品牌卷起来了
最新文章