当前位置:网站首页>Destructor of member function
Destructor of member function
2022-07-28 04:30:00 【Magic tea】
Definition :
Destructor : Contrary to constructor function , Destructors do not complete the destruction of objects , The destruction of local objects is done by the compiler . When the object is destroyed, it will automatically call the destructor , Complete some resource cleanup of the class
features :
1. The destructor name is preceded by the character ~.
2. No parameter, no return value .
3. A class has only one destructor . If not explicitly defined , The system will automatically generate the default destructor .
4. At the end of the object's lifecycle ,C++ The compiler system automatically calls the destructor
For example , Let's look at the following code
typedef int DataType;
class SeqList
{
public:
SeqList(int capacity = 10)
{
_pData = (DataType*)malloc(capacity * sizeof(DataType));
assert(_pData);
_size = 0;
_capacity = capacity;
}
private:
int* _pData;
size_t _size;
size_t _capacity;
};We all know that in general malloc After the space , We all need to use free To free up space .
And in practice , We sometimes overlook free, And run the code directly .
So for our convenience , Destructor here , It's equivalent to that the program automatically helps you make up one free come out .
Specific code :
typedef int DataType;
class SeqList
{
public:
SeqList(int capacity = 10)
{
_pData = (DataType*)malloc(capacity * sizeof(DataType));
assert(_pData);
_size = 0;
_capacity = capacity;
}
~SeqList()
{
if (_pData)
{
free(_pData); // Free up space on the heap
_pData = NULL; // Set the pointer to null
_capacity = 0;
_size = 0;
}
}
private:
int* _pData;
size_t _size;
size_t _capacity;
};
边栏推荐
- IPC: multi process binder Aidl user guide, including cases
- 仿真测试断开服务器公网连接
- 登录之后右上角改变 进入登录状态
- Sort - cardinal sort
- 《关于我写自定义cell这件事》
- Power consumption: leakage power
- [Niuke] find 1+2+3+... +n
- What is the account opening process of qiniu business school? Is it safe?
- 重要的 SQL Server 函数 - 其他函数
- 【sylar】框架篇-Chapter8-定时器模块
猜你喜欢

Efficient practice intelligent analysis of massive log data in aviation industry

could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node

重要的 SQL Server 函数 - 其他函数

Render the data obtained from the database to the table in elementui

MySQL:数据类型和运算符

Blooming old trees -- quickly build a map bed application with imageprocessor

空间复杂度计算超全整理!!(一起手撕复杂度计算

Null security and exception

Fedformer MOE module

H265/hevc noun explanation -- CTU, CTB, Cu, CB, Tu, PU, TB, Pb, LCU, slice, tile, chroma, luma, I frame, B frame, P frame
随机推荐
gerrit操作-回退掉某个patch_set
Information system project manager (2022) - key content: organization level project management, process management, project set management (18)
Render the data obtained from the database to the table in elementui
ESP8266 WIFI 模块和手机通信
Campus stray cat information recording and sharing applet source code
Use Baidu developer tool 4.0 to build a dedicated applet IDE
Important SQL server functions - other functions
[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]
Select sorting method
【实战】使用 Web Animations API 实现一个精确计时的时钟
A little advice for students - how to build their own knowledge system?
【二、移动web网页开发】2D&3D转换与动画、移动端布局、响应式布局
031 log4j log framework
Space complexity calculation super full sorting!! (calculation of hand tearing complexity
High number_ Chapter 4__ curvilinear integral
About me writing a custom cell
Notes to subject 2
Harmony's Application on the shelves reported an error. The solution of "please use the API of the released version to develop the application and apply for listing"
【sylar】框架篇-Chapter9-hook 模块
虚拟机类加载机制