In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.