Artificial block, C/C++ Programming

Assignment Help:

What if I cannot wrap the local in an artificial block? need help on Artificial Block in c++.

 


Related Discussions:- Artificial block

What is difference among macro and template?, A: In C++ there is a main dif...

A: In C++ there is a main difference among a template and a macro. Merely a macro is a string which the compiler replaces along with the value that was defined. For example #define

Hotel booking, Construct a console program to manage the booking of a Hotel...

Construct a console program to manage the booking of a Hotel room.

Explain input output function, Input Output Most languages have "statem...

Input Output Most languages have "statements" to perform I/O . Though in C and C++ we use "functions" to perform I/O. C++ also has its own I/O mechanism - the cin and cout obje

C program for compare two strings , Normal 0 false false fa...

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

What is the difference between = symbol and = = symbol, What is the differe...

What is the difference between = symbol and == symbol? - The = symbol is generally used in mathematical operations. It's used to assign a value to a given variable whereas the

I need vsti program, I need vsti program Project Description: Knowled...

I need vsti program Project Description: Knowledge of vsti programs, and music is a plus, ex: nexus I will input the sounds I just need the interface built Skills required

Program that implements a digital clock, Write C code for a program that im...

Write C code for a program that implements a digital clock (HH:MM:SS). The clock includes a two-digit hour indicator HH (00-23), a two-digit minute indicator MM (00-59), and a t

Define the arithmetic operators in c language, Define the Arithmetic Operat...

Define the Arithmetic Operators in c Language? There are five arithmetic operators in C and they are Operator             Purpose +                        Addition -

Oscar

2/13/2013 2:08:08 AM

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.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd