Destructors in C++ - C++ 中的类和对象析构函数

本文深入探讨C++中的析构函数,解释其在对象超出范围时如何自动调用以销毁对象并释放内存。通过实例展示析构函数的执行过程,并说明其与构造函数的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Destructors in C++ - C++ 中的类和对象析构函数

1. Destructors in C++

Destructors are functions which are just the opposite of constructors. In this chapter, we will be talking about destructors.
析构函数是与构造函数相反的函数。在本章中,我们将讨论析构函数。

We all know that constructors are functions which initialize an object. On the other hand, destructors are functions which destroy the object whenever the object goes out of scope.
我们都知道构造函数是用于初始化对象的函数。另一方面,析构函数是在对象超出范围时销毁对象的函数。

It has the same name as that of the class with a tilde (~) sign before it.
它的名称与该类的名称相同,前面带有波浪号 (~)。

//============================================================================
// Name        : std::class
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

class A
{
public:
	~A();
};

Here, ~A() is the destructor of class A.
在这里,~A() 是类 A 的析构函数。

Destructors don’t take any argument and have no return type.
析构函数不接受任何参数,也没有返回类型。

2. When is a destructor called?

A destructor gets automatically called when the object goes out of scope. We know that a non-parameterized constructor gets automatically called when an object of the class is created. Exactly opposite to it, since a destructor is also always non-parameterized, it gets called when the object goes out of scope and destroys the object.
当对象超出范围时,将自动调用析构函数。我们知道,创建类的对象时会自动调用非参数化的构造函数。与之完全相反,由于析构函数也总是非参数化的,因此当对象超出范围并销毁对象时,将调用该析构函数。

If the object was created with a new expression, then its destructor gets called when we apply the delete operator to a pointer to the object. We will learn more about new and delete in the chapter Dynamic Memory Allocation.
如果对象是使用新表达式创建的,则当我们将 delete 运算符应用于指向该对象的指针时,将调用其析构函数。我们将在动态内存分配一章中了解有关 newdelete的更多信息。

Destructors are used to free the memory acquired by an object during its scope (lifetime) so that the memory becomes available for further use.
析构函数用于释放对象在其作用域 (生命周期) 中获取的内存,以便使该内存可供进一步使用。

//============================================================================
// Name        : std::class
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;

class Rectangle
{
	int length;
	int breadth;

public:
	void setDimension(int l, int b)
	{
		length = l;
		breadth = b;
	}

	int getArea()
	{
		return length * breadth;
	}

	Rectangle()              // Constructor
	{
		cout << "Constructor" << endl;
	}

	~Rectangle()             // Destructor
	{
		cout << "Destructor" << endl;
	}
};

int main()
{
	Rectangle rt;

	rt.setDimension(7, 4);
	cout << rt.getArea() << endl;

	return 0;
}

Output

Constructor
28
Destructor

In this example, when the object rt of class Rectangle was created, its constructor was called, no matter in what order we define it in the class. After that, its object called the functions setDimension and getArea and printed the area. At last, when the object went out of scope, its destructor got called.
在这个例子中,当创建了类 Rectangle 的对象 rt 时,无论我们在类中定义它的顺序如何,都将调用其构造函数。之后,其对象调用函数 setDimensiongetArea并打印 area。最后,当对象超出范围时,将调用其析构函数。

Note that the destructor will get automatically called even if we do not explicitly define it in the class.
请注意,即使我们没有在类中明确定义析构函数,也会自动调用该析构函数。

The difference between ordinary and extraordinary is practice.
平凡与非凡之间的区别是实践。

inheritance [ɪnˈherɪtəns]:n. 继承,遗传,遗产
class:类
derived class:继承类,派生类
subclass:子类
base class:基类
superclass:超类,父类
passion [ˈpæʃn]:n. 激情,热情,酷爱,盛怒

References

https://www.codesdope.com/cpp-destructors/

资源下载链接为: https://pan.quark.cn/s/abbae039bf2a 无锡平芯微半导体科技有限公司生产的A1SHB三极管(全称PW2301A)是一款P沟道增强型MOSFET,具备低内阻、高重复雪崩耐受能力以及高效电源切换设计等优势。其技术规格如下:最大漏源电压(VDS)为-20V,最大连续漏极电流(ID)为-3A,可在此条件下稳定工作;栅源电压(VGS)最大值为±12V,能承受正反向电压;脉冲漏极电流(IDM)可达-10A,适合处理短暂高电流脉冲;最大功率耗散(PD)为1W,可防止器件过热。A1SHB采用3引脚SOT23-3封装,小型化设计利于空间受限的应用场景。热特性方面,结到环境的热阻(RθJA)为125℃/W,即每增加1W功率损耗,结温上升125℃,提示设计电路时需考虑散热。 A1SHB的电气性能出色,开关特性优异。开关测试电路及波形图(图1、图2)展示了不同条件下的开关性能,包括开关上升时间(tr)、下降时间(tf)、开启时间(ton)关闭时间(toff),这些参数对评估MOSFET在高频开关应用中的效率至关重要。图4呈现了漏极电流(ID)与漏源电压(VDS)的关系,图5描绘了输出特性曲线,反映不同栅源电压下漏极电流的变化。图6至图10进一步揭示性能特征:转移特性(图7)显示栅极电压(Vgs)对漏极电流的影响;漏源开态电阻(RDS(ON))随Vgs变化的曲线(图8、图9)展现不同控制电压下的阻抗;图10可能涉及电容特性,对开关操作的响应速度稳定性有重要影响。 A1SHB三极管(PW2301A)是高性能P沟道MOSFET,适用于低内阻、高效率电源切换及其他多种应用。用户在设计电路时,需充分考虑其电气参数、封装尺寸及热管理,以确保器件的可靠性长期稳定性。无锡平芯微半导体科技有限公司提供的技术支持代理商服务,可为用户在产品选型应用过程中提供有
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值