析构函数 C++:给“删除”的类型“***”参数,预期的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/382822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:03:27  来源:igfitidea点击:

Destructor C++: type '***' argument given to 'delete', expected pointer

c++destructor

提问by andandandand

I declared a private variable

我声明了一个私有变量

vector<SomeClass> theVector;

someplace inside my SomeClass class.

在我的 SomeClass 类中的某个地方。

Why can't I say: delete theVectorinside my SomeClass destructor?

为什么我不能说:delete theVector在我的 SomeClass 析构函数中?

The compiler error says:

编译器错误说:

 type `class Vector<SomeClass>' argument given to `delete', expected pointer 

What expected pointer?

什么预期的指针?

回答by Martin York

If new and delete go hand in hand.

如果新建和删除齐头并进。

To delete something you need to create it via new (which gives you a pointer). You can then delete the pointer. The way you are declaring the vector it is being created on the stack (not the heap) and will be deallocated when it goes out of scope.

要删除某些内容,您需要通过 new 创建它(它为您提供一个指针)。然后您可以删除指针。您声明向量的方式是在堆栈(而不是堆)上创建,并且在超出范围时将被释放。

int main()
{
    vector<SomeClass> theVector;

    vector<SomeClass>* ptrVctor = new vector<SomeClass>();


    delete ptrVctor;   // ptrVctor must be deleted manually
    // theVector destroyed automatically here
}

回答by paxdiablo

In C++ (unlike Java), you can create objects either on the stack or the heap. An example of creating it on the stack is, as you have done:

在 C++ 中(与 Java 不同),您可以在堆栈或堆上创建对象。在堆栈上创建它的一个例子是,正如你所做的:

vector<SomeClass> theVector;

This object goes out of scope when the stack frame disappears (normally when you return from the function that created the object.

当堆栈帧消失时(通常当您从创建该对象的函数返回时),该对象超出范围。

Creating objects on the heap allows them to outlive the function that created them and you do that by performing:

在堆上创建对象允许它们比创建它们的函数存活时间更长,您可以通过执行以下操作来实现:

vector<SomeClass> *theVectorPtr = new vector<SomeClass>();

You can then pass the theVectorPtrpointer back to the caller of the function (or store it globally, whatever you want).

然后,您可以将theVectorPtr指针传递回函数的调用者(或将其存储在全局范围内,无论您想要什么)。

In order to get rid of the object on the heap, you explicitly delete it:

为了摆脱堆上的对象,你明确地删除它:

delete theVectorPtr;

somewhere in your code.

在您的代码中的某处。

Deleting an object on the heap ends the scope of that object, the same way returning from a function ends the scope of variables created on the stack.

删除堆上的对象会结束该对象的作用域,就像从函数返回结束堆栈上创建的变量的作用域一样。

回答by Uri

If an object (rather than a value) is defined as a class member variable, then its storage is always tied to the object instance of that class.

如果一个对象(而不是一个值)被定义为一个类成员变量,那么它的存储总是与该类的对象实例相关联。

Therefore, if the containing object is allocated on the stack, then that object and the field will die when the stack unrolls.

因此,如果包含对象在堆栈上分配,那么当堆栈展开时,该对象和字段将消亡。

If the containing object is allocated on the heap, then the field object will die when the entire containing object dies with delete.

如果包含对象在堆上分配,那么当整个包含对象因删除而消亡时,字段对象也将消亡。

You will only be applying delete to a field if that is a pointer, since all that is stored with the containing object is the address of some other memory area, and you are deleting the materials in that area.

如果它是一个指针,您将只对字段应用删除,因为与包含对象一起存储的所有内容都是某个其他内存区域的地址,并且您正在删除该区域中的材料。

回答by Uri

To destroy all of the objects held in the vector, you would do the following:

要销毁向量中的所有对象,您可以执行以下操作:

theVector.resize(0);

This will happen automatically when the vector goes out of scope.

当向量超出范围时,这将自动发生。

回答by David Norman

The memory for theVector is part of the memory allocated for the SomeClass object, so you can't delete it without deleting the entire SomeClass object. The memory for theVector will get automatically freed when the SomeClass object is destructed.

TheVector 的内存是为 SomeClass 对象分配的内存的一部分,因此您不能在不删除整个 SomeClass 对象的情况下删除它。当 SomeClass 对象被破坏时,theVector 的内存将自动释放。

回答by PolyThinker

This is because theVector is not a pointer, which is what delete' expects. "Expected pointer" means the operand ofdelete' must be a pointer.

这是因为 theVector 不是指针,这就是delete' expects. "Expected pointer" means the operand ofdelete' 必须是指针。

Compare this to

将此与

int theInt;
delete theInt;

It surely will generate an error similar to what you got.

它肯定会产生类似于你得到的错误。

回答by yesraaj

c++ gives you flexibility to create object in stack and heap. When the object is created in heap through new operator as shown below it returns the pointer to the object in heap.

C++ 使您可以灵活地在堆栈和堆中创建对象。当对象通过 new 运算符在堆中创建时,如下所示,它返回指向堆中对象的指针。

ClassA * pobj_class = new ClassA();

For object created in stack the constructor returns the object rather than pointer as shown below.

对于在堆栈中创建的对象,构造函数返回对象而不是指针,如下所示。

ClassA obj_class();

and stack object automatically destroyed when variable(obj_class) goes out of scope,but object created on heap lives for ever.So to destroy heap object c++ gives you delete operator that takes pointer as argument and destroys the object the pointer is pointing to.

当变量(obj_class)超出范围时,堆栈对象会自动销毁,但在堆上创建的对象将永远存在。因此,要销毁堆对象,c++ 为您提供了删除运算符,该运算符将指针作为参数并销毁指针指向的对象。