The destructor is automatically invoked at the end of the object's life, and the familiar smart pointers are implemented according to the characteristics of the destructor, including the memory management mechanism of QT, which is implemented by the mechanism of destructor. C + + founder Bjarne Stroustrup in the creation of destructors is also for this purpose, it can be seen that if the destructor with good words, you could save us a lot of work, you no longer need to manually call the deleted object to use the heap memory, you just need to delete the heap into the destructor in the row, Because the destructor is called automatically when the object leaves its life cycle, the C + + language specification prescribes the invocation of the destructor:
Destructors is invoked implicitly (1) for a constructed object with static storage duration (3.7.1) at program Terminatio N (3.6.3), (2) for a constructed object with automatic storage duration (3.7.2) while the block in which the object is Crea Ted Exits (6.7), (3) for a constructed temporary object when the lifetime of the temporary object Ends (12.2), (4) for a C Onstructed object allocated by a newexpression (5.3.4), through with a deleteexpression (5.3.5), (5) in several situatio NS due to the handling of exceptions (15.3). A program was illformed if an object of class type or array thereof are declared and the destructor for the class are not ACC Essible at the point of the declaration. Destructors can also be invoked explicitly.
The effect is:
Destructors can be implicitly called in the following five ways:
(1) An object constructed in a static store (also known as a global object or a static object, which is placed in the data area of the program), and when the program ends, the object's destructor is automatically called.
(2) in an automatic storage area (also known as a local object, which is placed in the program's stack), the object's constructor is automatically invoked when the object being constructed leaves its area, such as an object declared within 1 functions, leaving the scope of the function.
(3) An object that is temporarily constructed when it leaves its life cycle, the destructor of the object is called, and (2).
(4) The object of the new construct (that is, the object is in the heap), deleted by Delete, the destructor is called
(5) In the case of try,catch handling exceptions, when the object in the try block, because of an exception, enters the catch branch, the constructor is called in the Catch branch
The above 5 are generated by the compiler by default call method, of course, there are 1 kinds of can be called by the way of the display, that is, like calling a function called destructors
With the above introduction, the next step into our subject, delete, but do not call the destructor of the case, this is the above mentioned in the C + + (4) specification, the following is a simple example to illustrate:
Example consists of 7 files, Testa.cpp,testa.h, testb.cpp,testb.h,testapp.h,testapp.cpp,main.cpp
[HTML]View PlainCopy
- Testa.h file
- #ifndef _test_a_h
- #define _test_a_h
- Class Ctesta {
- Public
- Ctesta ();
- ~ctesta ();
- };
- #endif
[HTML]View PlainCopy
- Testa.cpp file
- #include "Testa.h"
- #include <qdebug.h>
- Ctesta::ctesta ()
- {
- }
- Ctesta::~ctesta ()
- {
- Qdebug () << "~ctesta ()";
- }
[HTML]View PlainCopy
- testb.h file
-
- # ifndef _test_b_h
- #define &NBSP;_TEST_B_H&NBSP;&NBSP;
- class ctesta;
- class ctestb {
- PUBLIC:&NBSP;&NBSP;
- static ctestb *getinstance ();
- ctesta *gettesta ();
-
- PRIVATE:&NBSP;&NBSP;
- ctestb ();
- ~ctestb ();
-
- PRIVATE:&NBSP;&NBSP;
- ctesta *ptesta;
- &NBSP;&NBSP;&NBSP;&NBSP;STATIC&NBSP;CTESTB&NBSP;MSELF;&NBSP;&NBSP;
- };
- #endif
[HTML]View PlainCopy
- Testb.cpp file
- #include "Testa.h"
- Ctestb ctestb::mself;
- Ctestb *ctestb::getinstance ()
- {
- Return &mSelf;
- }
- CTESTB::CTESTB ()
- {
- Ptesta = new Ctesta ();
- }
- CTESTB::~CTESTB ()
- {
- Delete Ptesta;
- Qdebug () << "~CTESTB ()";
- }
- Ctesta *ctestb::gettesta ()
- {
- return ptesta;
- }
[HTML]View PlainCopy
- Testapp.h file
- #ifndef _test_app_h
- #define _test_app_h
- Class Ctesta;
- Class Ctestapp {
- Public
- Ctestapp (Ctesta *ptesta);
- ~ctestapp ();
- Private
- Ctesta *ptesta;
- };
- #endif
[HTML]View PlainCopy
- Testapp.cpp file
- #include "Testapp.h"
- #include <qdebug.h>
- #include "Testa.h"
- Ctestapp::ctestapp (Ctesta *ptesta)
- {
- this->ptesta = Ptesta;
- }
- Ctestapp::~ctestapp ()
- {
- Delete Ptesta;
- Qdebug () << "~ctestapp ()";
- }
[HTML]View PlainCopy
- Main.cpp file
- #include "Testb.h"
- #include "Testcpp.h"
- int main (int argc, char *argv[])
- {
- Qapplication app (argc, argv);
- CTESTB *ptestb = ctestb::getinstance ();
- Ctestapp *Ptestapp = new Ctestapp (ptestb->gettesta ());
- Delete Ptestapp;
- return App.exec ();
- }
Here are the output results,
~ctestapp ()
Description Delete Ptesta, after not called the Ptesta destructor, when the Testapp.cpp file//#include "testa.h" comment cancellation, delete will call Ptesta destructor, which is the output after the comment is removed:
~ctesta ()
~ctestapp ()
Note above the compilation environment is Ming32-make, do not know the other compilation environment will not appear so problem, this left to everyone to verify.
Here is the disassembly code, which adds the testa.h delete Ptesta disassembly code to the Testapp.cpp:
Delete Ptesta;
0x0040255c <+8>: mov 0x8 (%EBP),%eax
0x0040255f <+11>: mov (%eax),%EBX
0x00402561 <+13>: test %EBX,%EBX
0x00402563 <+15>: JE 0x402575 <~CTestApp+33>
0x00402565 <+17>: mov %ebx, (%ESP)
0x00402568 <+20>: call 0x403082 <~CTestA>
0x0040256d <+25>: mov %ebx, (%ESP)
0x00402570 <+28>: call 0x40aa68 <_ZdlPv>
This is the delete Ptesta disassembly code without testa.h in Testapp.cpp:
Delete Ptesta;
0x00402550 <+8>: mov 0x8 (%EBP),%eax
0x00402553 <+11>: mov (%eax),%eax
0x00402555 <+13>: mov %eax, (%ESP)
0x00402558 <+16>: call 0x40aa48 <_ZdlPv>
You can see the testa.h in the disassembly, called the destructor ~ctesta, call 0x403082 <~CTestA>
http://blog.csdn.net/rabinsong/article/details/9347615
Delete, the destructor does not call