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

Array of Objects A class is a template, which can contain data items as well as member functions to operate on the data items. Various objects of the class can also be declared

how to write the code for operation research


Problem : (a) Stacks can be implemented using arrays. Write down the following C or C++ function to (i) create a stack (ii) add an element to a stack (push). (b) Expl

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

In general, Roman numerals can be converted mathematically by simply assigning a numerical value to each letter, according to the chart below, and calculating a total: M=1000 | D=5

Create Complex number using constructor: class complex                                 {                                                   private:

how to write c code to solve assignment problem?

Write a program to determine V c for a given value of time (t)   #include stdio.h #include math.h void main() {      char prompt;   float vs,cr,t,vc;   /* input time */