What if one can''t wrap the local in an artificial block?, C/C++ Programming

What if one can''t wrap the local in an artificial block?

Posted Date: 3/15/2013 5:33:50 AM | Location : United States





What if one can''t wrap the local in an artificial block?

A: Mostly time, one can limit the lifetime of a local via wrapping the local in an artificial block ({...}). But if for some cause you can''t do that, add a member function which has a similar effect as the destructor. Although do not call the destructor itself!

For instance, in the case of class File, you may add a close() method. The destructor typically will simply call this close() method. Note down that the close() method will have 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 may check at the starting 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 down that the other File methods might also have to check if the fileHandle_ is -1 (that mean., check if the File is closed).

Note down  also that any constructors that don''t actually open a file should set fileHandle_ to -1.

 

Posted by | Posted Date: 3/15/2013 5:35:15 AM


Related Discussions:- What if one can''t wrap the local in an artificial block?, Assignment Help, Ask Question on What if one can''t wrap the local in an artificial block?, Get Answer, Expert's Help, What if one can''t wrap the local in an artificial block? Discussions

Write discussion on What if one can''t wrap the local in an artificial block?
Your posts are moderated
Related Questions
Consider the following example: int max(int x, int y)                  {                                 return ( x > y) ? x : y ;                  }   float max

what are 3 conditions under which the sequential search of a list is preferable to binary search?

Write a Program to illustrate the Call by Reference? Here is an illustration: #include . int compute_sum(int *n); int main( void) { int n=3, sum; printf("%d\n",n); /

Sine function computation

Write a Haskell program that calculates a balanced partition of N items where each item has a value between 0 and K such that the difference between the sum of the values of first

YOU HAVE GIVEN THE MARKED UP PRICE OF 80% AND DISCOUNT PRICE OF 10% THEN FIND THE SELLING PRICE

Explain the Loop Statements in Computer Programming? 1. C gives you a choice of three kinds of loop, while, do while and for. 2. The while loop remain repeating an action until

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''''a''''-''''z'''') and

given integer ''n'' and permutation of numbers 1,2,..n-1, c program to print permutation that lexicograghically precedes given input permutation if permutation is least print input

Define Polymorphism? A: Polymorphism let a client to treat di_erent objects in the simialr way even if they were developed from di_erent classes and exhibit di_erent behaviors.