Wednesday 22 February 2012

What happens if the local variable cannot be wrapped in an artificial block? | Constructor in C++


Most of the times, the time duration of wrapping local variables is limited in an artificial block ({...}). In other situations, a member function with similar effects can be added as a destructor instead of calling the destructor itself.For example, in the case of class of Filel, a close() method can be added. Usually, the close() method can be simply called by the destructor. Note that there is a need of marking Filel by the close method(). It avoids the reclosing of an already closed file by a subsequent call of a destructor. For example, the value of the filelHandle can be set by the call of a destructor, such as -1 and the checking can be done at the beginning for ensuring that the filelHandle is already equal to value (-1):
PROGRAM::
Note that the other file methods may also need to check if the filelHandle_ is -1 (i.e., check if the Filel is closed).
Also note that any constructors that do not actually open a file should set filelHandle_ to -1.