什么时候调用C ++析构函数? [英] When is a C++ destructor called?

查看:187
本文介绍了什么时候调用C ++析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本问题:什么时候程序在C ++中调用类的析构函数方法?我被告知,当对象超出范围或受到删除

Basic Question: when does a program call a class' destructor method in C++? I have been told that it is called whenever an object goes out of scope or is subjected to a delete

具体问题:

1)如果对象是通过指针创建的,并且该指针以后被删除或给出一个新的地址指向它,它所指向的对象调用它的析构函数(假设没有别的东西指向它)?

1) If the object is created via a pointer and that pointer is later deleted or given a new address to point to, does the object that it was pointing to call its destructor (assuming nothing else is pointing to it)?

2)继续问题1,什么定义什么时候一个对象超出范围当对象离开给定的{block}时)。所以,换句话说,什么时候是一个析构函数调用一个链接列表中的对象?

2) Following up on question 1, what defines when an object goes out of scope (not regarding to when an object leaves a given {block}). So, in other words, when is a destructor called on an object in a linked list?

3)你是否想手动调用析构函数?

3) Would you ever want to call a destructor manually?

推荐答案


1)如果对象是通过指针创建的,并且该指针以后被删除或给出一个新的地址指向它的对象,它指向的对象调用其析构函数(假设没有其他指向它)?

1) If the object is created via a pointer and that pointer is later deleted or given a new address to point to, does the object that it was pointing to call its destructor (assuming nothing else is pointing to it)?

指针的类型。例如,智能指针经常在删除对象时删除它们。普通指针不。当指针指向不同的对象时也是如此。一些智能指针会破坏旧的对象,或者如果没有更多的引用,它将被破坏。普通指针没有这样的智能。他们只是持有一个地址,并允许你对他们指向的对象执行操作,具体这样做。

It depends on the type of pointers. For example, smart pointers often delete their objects when they are deleted. Ordinary pointers do not. The same is true when a pointer is made to point to a different object. Some smart pointers will destroy the old object, or will destroy it if it has no more references. Ordinary pointers have no such smarts. They just hold an address and allow you to perform operations on the objects they point to by specifically doing so.


2) 1,什么定义对象何时超出范围(不关心对象何时离开给定的{block})。所以,换句话说,什么时候是一个析构函数调用一个链接列表中的对象?

2) Following up on question 1, what defines when an object goes out of scope (not regarding to when an object leaves a given {block}). So, in other words, when is a destructor called on an object in a linked list?

列表。典型的集合在销毁时会销毁所有包含的对象。

That's up to the implementation of the linked list. Typical collections destroy all their contained objects when they are destroyed.

因此,链接的指针列表通常会销毁指针,而不是它们指向的对象。 (这可能是正确的,它们可能是其他指针的引用)。但是,专门设计为包含指针的链表可能会删除对象本身的破坏。

So, a linked list of pointers would typically destroy the pointers but not the objects they point to. (Which may be correct. They may be references by other pointers.) A linked list specifically designed to contain pointers, however, might delete the objects on its own destruction.

智能指针的链接列表可以在指针被删除时自动删除对象,或者如果没有更多的引用,则可以这样做。

A linked list of smart pointers could automatically delete the objects when the pointers are deleted, or do so if they had no more references. It's all up to you to pick the pieces that do what you want.


3)你是否想手动调用析构函数? / p>

3) Would you ever want to call a destructor manually?

当然。一个例子是,如果你想用另一个相同类型的对象替换一个对象,但不想释放内存,只是再次分配它。你可以销毁旧的对象就地,并构建一个新的地方。 (但是,通常这是一个坏主意。)

Sure. One example would be if you want to replace an object with another object of the same type but don't want to free memory just to allocate it again. You can destroy the old object in place and construct a new one in place. (However, generally this is a bad idea.)

// pointer is destroyed because it goes out of scope,
// but not the object it pointed to. memory leak
if (1) {
 Foo *myfoo = new Foo("foo");
}


// pointer is destroyed because it goes out of scope,
// object it points to is deleted. no memory leak
if(1) {
 Foo *myfoo = new Foo("foo");
 delete myfoo;
}

// no memory leak, object goes out of scope
if(1) {
 Foo myfoo("foo");
}

这篇关于什么时候调用C ++析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆