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
In this project, you will write a software for the Registrar's office of a university called YouOIT to maintain information about students and courses at the university. The reg

demonstrates shearing about origin


Declaration of Variables: Variables are declared as follows: int a; float b;   Assigning value to variables: int a = 100; Declaring and assigning is called ini

A: No.       While realloc() need to copy the allocation, this uses a bitwise copy operation, that will tear several C++ objects to shreds. C++ objects must be allowed to copy t

WHAT IS CGI?

C Program for FUNCTION OF MERGE #include conio.h> #include stdio.h> char cot(char a[],char b[]); void main() {           char a[50],b[50];           clrscr()

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Explain Operators in C Language? The C language is very rich in built - in - operators and it places more significance on operators than do most other computer languages. The C