Get the App
SLTechnology News&Howtos  ›  Development  › 

A case study of the destructor of C++ object destruction

Shulou Source: shulou.com Published: 2022-05-31 21:12:43 10月25日 Update

In this article, the editor introduces in detail "the destructor case analysis of the destruction of C++ objects" with detailed contents, clear steps and proper handling of details. I hope that this article "destructor case analysis of the destruction of C++ objects" can help you solve your doubts.

I. destruction of objects

The objects in life are not listed until they are initialized.

Some cleaning work will be done before the objects in life are destroyed.

-as far as unit is concerned, all objects that need to be destroyed should be cleaned up.

Solution

Provide a free function of public for each class

Call the free function to clean up when the object is no longer needed

As follows:

Existing problems

Free is just a normal function, a call that must be displayed

The object is not cleaned before it is destroyed, which is likely to cause resource leakage.

Can the C++ compiler automatically call a special function to clean up objects?

2. Destructor function

A special cleanup function can be defined in the class of C++

This special cleanup function is called a destructor.

The function of destructor is opposite to that of constructor.

Definition: ~ ClassName ()

The destructor has no arguments and no return value type declaration

The destructor is called automatically when the object is destroyed

Let's start by simply using destructors:

# include class Test {public: Test () {printf ("Test ()\ n");} ~ Test () {printf ("~ Test ()\ n");}}; int main () {Test t; return 0;}

The output is as follows:

Although t is an object, it is also a local variable in nature, which is destroyed before return 0, and the destructor is called automatically when t is destroyed.

Let's look at another example:

# include class Test {int mi;public: Test (int I) {mi = I; printf ("Test ():% d\ n", mi);} ~ Test () {printf ("~ Test ():% d\ n", mi);}}; int main () {Test t (1); Test* pt = new Test (2); delete pt; return 0;}

The output is as follows:

Definition criteria of destructor

When a custom constructor is defined in the class, and system resources are used in the constructor (such as ∶ memory request, file opening, etc.), a custom destructor is required.

Let's take a look at another experiment:

IntArray.h:

# ifndef _ INTARRAY_H_#define _ INTARRAY_H_ class IntArray {private: int masked pooling; int* massively pointering public: IntArray (int len); IntArray (const IntArray& obj); int length (); bool get (int index, int& value); bool set (int index, int value); ~ IntArray ();}; # endif

IntArray.cpp:

# include "IntArray.h" # include "stdio.h" IntArray::IntArray (int len) {m_pointer = new int [len]; for (int iTuno; I

Tags: Functions objects clocking output examples instance analysis special articles results content resources life proper ordinary that is examples memory criteria Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno vpn Docker Xiaomi