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
Define a user-defined data type (struct) called Car to represent a car in the context of a car dealership database. Car must have at least the following characteristics: Make Model

Default assignment operator mange assigning one object to another object of the same class. It is member to member copy as shallow copy.

Can any constructor throw an exception? How to handle error while the constructor fails?


What is constructor or ctor? need help

Ask question # Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particu

Ravi is a newbie to the programming and while learning the programming language he came to know the following rules:

Ques.Implement a c/c++ program to find a shortest path between two nodes in a network?network should be taken as an adjacency matrix.

One person who is specialist at programming and solving problems with a computer Project Description: Dear Potential computer, hardware, programming and software genius, I

Program to find a Greatest String: C - Program: Write a program find largest string by c program. int main( int argc, char *argv[] ) {     if( argc         {