尽量看,加油哦!-by xaut37 U1 b0 {; @1 w- |# c0 H1 Z
Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted. , S$ ~* ^, @& G" }! T" C+ `A destructor is a member function with the same name as its class prefixed by a ~ (tilde). For example: class X { ; {; h6 M) @2 H v% g4 Ppublic:' m8 I4 y# u# W
// Constructor for class X& a2 z7 m+ j8 S$ b; I
X();7 Z/ J: ^7 ?2 u3 S/ t! ~
// Destructor for class X ( N" R2 m% f2 A7 t Z2 ? ~X(); 3 {' G2 ^. w6 c2 U9 P" ~' z}; 9 @* t% N& W( o) ?, H* PA destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared const, volatile, const volatile or static. A destructor can be declared virtual or pure virtual.# ?! }) M( O3 ?0 ^# O
If no user-defined destructor exists for a class and one is needed, the compiler implicitly declares a destructor. This implicitly declared destructor is an inline public member of its class.; ^+ N+ \. F$ i( W# C
The compiler will implicitly define an implicitly declared destructor when the compiler uses the destructor to destroy an object of the destructor's class type. Suppose a class A has an implicitly declared destructor. The following is equivalent to the function the compiler would implicitly define for A: A::~A() { } e4 b/ ?+ r7 W! C$ tThe compiler first implicitly defines the implicitly declared destructors of the base classes and nonstatic data members of a class A before defining the implicitly declared destructor of A 4 K& F% m8 U" b! k9 E2 ZA destructor of a class A is trivial if all the following are true:
It is implicitly defined
All the direct base classes of A have trivial destructors
The classes of all the nonstatic data members of A have trivial destructors
$ Z: X z" e5 ?' `' y$ q9 n
If any of the above are false, then the destructor is nontrivial. : R1 r9 j) v1 [A union member cannot be of a class type that has a nontrivial destructor. 6 S1 K4 U, \5 s6 I4 I; }. iClass members that are class types can have their own destructors. Both base and derived classes can have destructors, although destructors are not inherited. If a base class A or a member of A has a destructor, and a class derived from A does not declare a destructor, a default destructor is generated.4 N( m- Z4 @. @, q! @) t$ W/ u/ @
The default destructor calls the destructors of the base class and members of the derived class. ! I% Y+ u% j5 w- e3 v- qThe destructors of base classes and members are called in the reverse order of the completion of their constructor:
The destructor for a class object is called before destructors for members and bases are called.
Destructors for nonstatic members are called before destructors for base classes are called.
Destructors for nonvirtual base classes are called before destructors for virtual base classes are called.
i' ~( W* x1 JWhen an exception is thrown for a class object with a destructor, the destructor for the temporary object thrown is not called until control passes out of the catch block.: a6 e) N* R9 [% C5 T
Destructors are implicitly called when an automatic object (a local object that has been declared auto or register, or not declared as static or extern) or temporary object passes out of scope. They are implicitly called at program termination for constructed external and static objects. Destructors are invoked when you use the delete operator for objects created with the new operator. 3 p9 H9 Y& O4 c# }For example: #include <string> 6 v t8 t1 D% ~ / _) m4 @- y: ~% [0 }7 S0 p3 l1 H0 a6 kclass Y {5 N) a8 m X& s4 I% T1 Y3 z
private: " I& G3 h3 m: @- W9 C0 F. C char * string; : G# V! X# O h2 m int number;3 p( p! i* W& U
public: * l7 e2 _! ?" J9 P // Constructor + ~% o, ?' u+ [/ u: Q Y(const char*, int); , ^; m1 G* m' z2 Z // Destructor . r$ ~ X" d( F5 R2 G ~Y() { delete[] string; } ( }9 m/ l1 M* v- T}; + D/ `% J! L/ \; h0 f2 c; E ' V& k9 ?& R8 q7 w y) H0 e// Define class Y constructor- |2 j$ h! U4 E" [
Y::Y(const char* n, int a) { ; A9 T0 M. Y9 ]5 J string = strcpy(new char[strlen(n) + 1 ], n);( m( D2 g$ }& T U" B, ]) G& x4 r4 Z9 Q
number = a; * v6 j/ W6 j q0 B1 N& \$ O' K}; F, d) W* O9 Z6 o! ~; M
) a7 ?& `: w6 V
int main () { ; x" D) P; r, I* i% k! U; @ // Create and initialize ; `6 |$ M: `: A2 M5 ~ // object of class Y$ Z0 z7 {( p5 D) i2 S
Y yobj = Y("somestring", 10);" b0 V6 B* @, |6 o7 K4 C
" e6 h/ S# F' H- b9 @! f: j // ...; }9 ?# P/ x7 k
: Q! p* p( ~* V; d& J) Y+ y, C5 P3 W$ D // Destructor ~Y is called before' l% b0 t# {( Z, |: a- Y
// control returns from main() + O1 {" t% H; M4 o& t} $ K% R! S/ O' p! cYou can use a destructor explicitly to destroy objects, although this practice is not recommended. However to destroy an object created with the placement new operator, you can explicitly call the object's destructor. The following example demonstrates this: #include <new>2 M9 @4 E. O; u- {' x* x
#include <iostream> / @- c% T V O; {% Jusing namespace std;4 `0 v# L$ u' o: _- A7 n8 i
class A { " c2 K/ u0 q1 d% O0 [2 I public: ! Z t7 Z, V, Z2 b1 N; m9 j2 X A() { cout << "A::A()" << endl; } 3 Q2 O% @! j+ X$ O9 g8 {+ P ~A() { cout << "A::~A()" << endl; }" w$ i6 h8 E2 R
};! X3 i! r* B* F& ]
int main () {1 Q1 _0 R( S* ?$ _& [7 J" i
char* p = new char[sizeof(A)]; ' W/ @3 l8 g _8 x0 n* h/ T0 a8 a/ f A* ap = new (p) A; % U5 q% n# a, O& n1 N; V ap->A::~A(); - S* A3 ~9 |' B# V delete [] p; 4 j7 [1 y% J' e. c0 i, R}2 L0 @& [( b7 n6 `, Y
The statement A* ap = new (p) A dynamically creates a new object of type A not in the free store but in the memory allocated by p. The statement delete [] p will delete the storage allocated by p, but the run time will still believe that the object pointed to by ap still exists until you explicitly call the destructor of A (with the statement ap->A::~A()). : k( m* e4 V: K1 { ]3 G/ qNonclass types have a pseudo destructor. The following example calls the pseudo destructor for an integer type: typedef int I; 1 e! {5 L3 E& w$ R( `+ y3 Vint main() {0 `, w. m/ C, L3 P% d. y
I x = 10; $ |; _# a( ]4 `5 ^5 D x.I::~I();8 U3 T& m, F7 N* H: P X
x = 20;; P/ v; j. X" A* L! f( Q# `
} : [' ^' s+ Y/ o4 ^( L aThe call to the pseudo destructor, x.I::~I(), has no effect at all. Object x has not been destroyed; the assignment x = 20 is still valid. Because pseudo destructors require the syntax for explicitly calling a destructor for a nonclass type to be valid, you can write code without having to know whether or not a destructor exists for a given type.