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 {


void close();




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





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
Determine the size of an interger data type without using sizeof() function? A: #include int main() { int *i ; int *j = i + 1; cout }

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 betw .

why oops

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

What are  Id e n t i f ie rs a n d C o n s t a n ts in C++? Id e n t i f ie r a n d C o n s t a n t : I d e n t i f ie r

write a program that counts the number of occurrences of the string in the n-th padovan string p(n)   program 1 : package test.padovanstring; public class PadovanString {

Pointer Types Pointer holds the address of an object, permitting for the indirect manipulation of that object. They are used in creating linked data structures like lists, tree

Implementing Operator Functions The general format of the Operator function is: return_type operator op ( argument list );   Where op is the symbol for the operator be

em Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using ''Content here, content here'', making it look like readable English. Many desktop publis