Constructor and Destructor - Practical Electron Microscopy and Database - - An Online Book - |
||||||||||||||
Microanalysis | EM Book http://www.globalsino.com/EM/ | ||||||||||||||
================================================================================= | ||||||||||||||
In DM and C++, it is very common that some part of an object to require initialization before it can be used. For instance, considering a class, before the class could be used, the corresponding variables have to be set to zero. Because the requirement for initialization is so common, DM allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor. Classes in DM can have constructors and destructors, which are specified by methods with no return type ( not even void), and only a single argument. A constructor is a special function that is a member of a class and that has the same name as the class. For instance, Table 1109a shows how the classes look when they are converted to use a constructor for initialization. Table 1109a. Comparison between scripts with and without constructors and destructors for initialization.
The constructor's name is the class name, and the destructor's name is the class name prepended with a '~'. An object’s constructor is called when the object is created. Therefore, it is called when the object's declaration is executed. For global objects, the constructor is called when the program starts to execute, prior to the call to main( ). For local objects, the constructor is called each time the object declaration is really encountered. The complement of the constructor is the destructor. In many circumstances, an object
will need to perform some action or series of actions when it has been destroyed. For instance, local objects
are created when their block is entered, and destroyed when the block is left. However, global
objects are destroyed when the program terminates. There are many reasons why a
destructor may be needed, for instance: When an object is created, then the constructors of all of its ancestor classes are called, from least derived to most derived, and when it is destroyed, the destructors are called in reverse order. If a constructor throws an exception, destructors are called for each fully contstructed ancestor class, and the exception is propagated out of the allocation statement. However, destructors are not allowed to throw an exception. An exception may be thrown from a statement within the destructor if it is caught and handled in the destructor. In C++, a constructor can have parameters, which allows you to give member variables program-defined initial values when an object is created. This can be done by passing arguments to an object's constructor. However, in DM, a constructor cannon have parameters, for instance, if the script below is used, the an error message will show up. On the other hand, in C++, unlike constructors, destructors normally do not have parameters. The reason is that there is no means by which to pass arguments to an object that is being destroyed. However, you can still have it in the program if it is needed for some reason. Table 1109b. Error message along with parameterized constructor.
Passing-Objects-to-Functions (see page1110) can be used in CLASS. In this case, an object can be passed to a function in the same way as any other data type, namely a copy of the object, not the actual object itself, is passed
to the function. This copy
becomes the parameter in the function, so that a new object comes into
existence. When the function terminates, the copy of the argument as well as the parameter
is destroyed. In C++, when a copy of an argument is made during a function call, then the normal constructor
is not called. Instead, the object’s copy constructor is actually called and the copy constructor defines
how a copy of an object is made. However, if a class does not clearly define a copy constructor, C++ provides one by default. The default copy constructor creates a bit-wise copy of the object. The reason is that it must not be called to make a copy of an already existing object since a normal constructor is used to initialize some aspect of an
object. In fact, such a call
would alter the contents of the object. However, when passing an object to a function, the current state of the object, not its initial state, is needed.
On the other hand, when the function terminates and the copy of the object used as an argument
is destroyed, the destructor is then called because the object has gone out
of scope. In this case, the preceding program can have two calls to the destructor:
|
||||||||||||||
================================================================================= | ||||||||||||||
|
||||||||||||||