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

Padovan string, find the occurence of zy in the final string

find the occurence of zy in the final string

Restart, how to create program in c that will system restart

how to create program in c that will system restart

Generate a class node that contains an integer id, Generate a class node ...

Generate a class node that contains an integer id, a position (x, y)  and a vector of 0 5,  generate a set of  x nodes each with random connectivity n.    Implement an algorith

Big o notation, The probabilistic Hough transform uses random sampling inst...

The probabilistic Hough transform uses random sampling instead of an accumulator array. In this approach the number of random samples r, is not specified in the OpenCV call, but is

Bus Connection, how to explain working of bus connection

how to explain working of bus connection

Explain public and private members, Public, Private and Protected members: ...

Public, Private and Protected members: Class members can either be declared in public','protected' or in the 'private' sections of the class. But as one of the features of OOP i

C program coding, write a c program to compute a padovan string

write a c program to compute a padovan string

Queue, write a queue program in c langauge?

write a queue program in c langauge?

Recursive functions, a program to determine whether a number is an odd or e...

a program to determine whether a number is an odd or even using recursive function

MCQ, in a multilist organisation

in a multilist organisation

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