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

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/

发布了454 篇原创文章 · 获赞 1733 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104406738