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 {

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 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
Differentiate between functions getch () and getche (). - Both functions accept a character input value from user. - When getch () is used, key that was pressed won't appear

People sometimes give their phone numbers using one or more alphabetic characters. Create a program that converts the alphabetic characters to their corresponding numbers. For exam

Problems With Multiple Inheritance The following example presents a problem with multiple inheritance. class Aclass  {   public :  void put()


Abstraction is of the process of hiding unwanted details from the user

Write a function that calculates the number of elapsed days between two dates. For example the days between Feb 3, 1970 and June 21, 1980? Becareful for the Leap year

polishing the jewel game with c coding

Queue - C++ program: Write a program to show the basic operations on queue. namespace stack {  const int max_size = 200;  char v(max_size);  int top=0;  void pu

programme for time table in c++

2. a) Define a structure to store roll no, name and marks of a student. b) Using the structure of Q2. a), above write a ''C'' program to create a file "student.dat". There must be