3.8 OBJECT DESTRUCTION


3.8 OBJECT DESTRUCTION

When objects go out of scope[7] in C++, they are automatically destroyed by the invocation of their destructors. A destructor is given the name of the class prefixed with a tilde. It should not return a value, neither should it take a parameter, as in the following example where we have supplied class Y with a destructor of its own in line (C). If a class has not been supplied explicitly with a destructor, the default meaning of the destructor is invoked for objects made from that class. The default meaning is to invoke the destructors for each of the data members of the class. For data members of the primitive types, destruction simply means freeing up the memory occupied by them.

 
//Dest.cc #include <iostream> using namespace std; class X {}; class Y { X* p; //(A) public: Y( X* q ) : p( new X(*q) ){} //(B) ~Y(){ delete p; } //(C) }; int main() { X* px = new X(); //(D) Y y( px ); //(E) delete px; //(F) return 0; }

As the variable y in line (D) goes out of scope when the flow of execution hits the right brace of main, Y's destructor in line (C) will be invoked automatically.

As will be explained in greater detail in Chapter 11, programmer-supplied destructors are needed particularly when an object appropriates system resources that need to be freed up before the object is destroyed. In our example here, construction of an object of type Y in line (B) entails allocating memory for an object of type X to which the data member p points. The system supplies a class with a default destructor if the programmer does not provide one. If we had not supplied Y with a destructor as shown in line (C), its default destructor would have just freed up the four bytes occupied by the pointer data member p, but not the memory to which p points.[8]

Java's object destruction works very differently from C++. If no variables in a Java program are holding references to an object, that object becomes a candidate for what is known as garbage collection. The garbage collector runs continuously in the background in a separate but low-priority thread. To somewhat compensate for the fact that it is beyond the control of a programmer to reclaim the memory occupied by an object at the very instant it becomes unreferenced, Java gives the programmer the option of asking the garbage collector to make a best effort to right away reclaim the space occupied by all unreferenced objects by invoking

      System.gc(); 

Before actually destroying an object, the garbage collector executes the code in the finalize() method of the object assuming the class was given such a method. The finalize() method does for Java what a programmer-supplied destructor does for C++-it can be used to close I/O connections, files, and so on. Chapter 11 goes more deeply into Java's garbage collection and object finalization.

[7]See Section 7.7 of Chapter 7 for a discussion on the scope of an identifier in C++. As mentioned there, the scope of an identifier is that part of a program in which an identifier is recognized as declared.

[8]The invocation X (*q) in line (B) is actually a call to the copy constructor of class X for constructing a new object of type X as a copy of a previously constructed object of the same type. Since X does not possess a programmer-defined copy constructor, its default meaning will be used, which is to make a byte-by-byte copy. The concept of a copy constructor in discussed in detail in Chapter 11.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net