Artificial block, C/C++ Programming

What if I cannot wrap the local in an artificial block? need help on Artificial Block in c++.

 

Posted Date: 2/13/2013 1:46:23 AM | Location : United States





Artificial block

Several times, you can limit the lifetime of a local by wrapping the local in an artificial block ({...}). But if for a number of reasons you cannot do that, add a member function that has a similar effect as the destructor. But do not call the destructor itself!  

For example, in the case of class File, you might add a close() method. Usually the destructor will simply call this close() method. Note that the close() method will need to mark the File object so a subsequent call won''t re-close an already-closed File. For example it might set the fileHandle_ data member to some nonsensical value like -1, and it might check at the beginning to see if the fileHandle_ is already equal to -1:  

class File {

public:

void close();

~File();

...

private:

int fileHandle_; // fileHandle_ >= 0 if/only-if it''s open

}; 

File::~File()

{

close();

void File::close()

{

if (fileHandle_ >= 0) {

...insert code to call the OS to close the file...

fileHandle_ = -1;

}

Note that the other File methods may also need to check if the fileHandle_ is -1 (i.e., check if the File is closed).  Note also that any constructors that don''t actually open a file should set file Handle_ to -1.

Posted by Oscar | Posted Date: 2/13/2013 2:08:08 AM


Related Discussions:- Artificial block, Assignment Help, Ask Question on Artificial block, Get Answer, Expert's Help, Artificial block Discussions

Write discussion on Artificial block
Your posts are moderated
Related Questions

Unions A union is also like a structure, except that only single variable in the union is stored in the allocated memory at a time. It is a collection of mutually exclusive var

in this problem u given a board in which some of the elements are placed as shown in diagram below .each element represent a color .fill the other elements in the board such that n

S y m b o li c c o n s t a n t : c on s t m a x = 1 0 0 ; T h e d e f a u l t d a t a t y p e w i l l b e i n t e

People sometimes give their phone numbers using one or more alphabetic characters. Create a program that converts the alphabetic characters to their corresponding numbers. For exam

What is the use of default constructor? - It's a constructor that doesn't accept any parameters. - If there is no user-defined constructor for a class, compiler declares a d

Write a program that predicts users’ age (0-128 years old) with at most 7 questions. The game starts with asking the user whether he/she is younger or older than G (an initial gues

Object Oriented Programming Paradigm: For OOP, Paradigm is a standard set.  The OOP will help to develop software in modules, which will be integrated later as a full packag

t a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with these

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b