C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Should resource wrappers auto cleanup on destruct?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Julie
Guest





PostPosted: Thu Jan 27, 2005 11:42 pm    Post subject: Should resource wrappers auto cleanup on destruct? Reply with quote



(I searched the FAQ, but didn't find anything relevant -- if there is, please
post a link.)

Is there a design paradigm that indicates that a class that manages an external
resource should automatically perform clean up in the destructor?

For example, suppose the following file-wrapper class.

// begin

class file
{
private:
int fileHandle;

public:
file()
{
fileHandle = 0;
}
void open(char * fileName)
{
fileHandle = MySystemIO::OpenFile(fileName);
}
void close()
{
if (fileHandle)
{
MySystemIO::CloseFile(fileHandle);
fileHandle = 0;
}
}
~file()
{
// call close() here or not?
// close();
}
}

// end

I realize that from a /documentation/ standpoint, I can do either and just
document wether or not the destructor performs cleanup

-- however --

I'm mostly interested in a self-documenting code perspective, where (external)
documentation isn't referenced.

Comments???
Back to top
red floyd
Guest





PostPosted: Fri Jan 28, 2005 12:11 am    Post subject: Re: Should resource wrappers auto cleanup on destruct? Reply with quote



Julie wrote:
Quote:
(I searched the FAQ, but didn't find anything relevant -- if there is,
please post a link.)
[redacted]

Google for "RAII" (Resource Acquisition Is Initialization).


Back to top
Mike Wahler
Guest





PostPosted: Fri Jan 28, 2005 12:26 am    Post subject: Re: Should resource wrappers auto cleanup on destruct? Reply with quote




"Julie" <julie (AT) nospam (DOT) com> wrote

Quote:
(I searched the FAQ, but didn't find anything relevant -- if there is,
please
post a link.)

Is there a design paradigm that indicates that a class that manages an
external
resource should automatically perform clean up in the destructor?

Yes. Stroustrup's "RAII" ("Resource Acquisition Is Initialization").

Quote:
For example, suppose the following file-wrapper class.

// begin

class file
{
private:
int fileHandle;

public:
file()
{
fileHandle = 0;
}
void open(char * fileName)
{
fileHandle = MySystemIO::OpenFile(fileName);
}
void close()
{
if (fileHandle)
{
MySystemIO::CloseFile(fileHandle);
fileHandle = 0;
}
}
~file()
{
// call close() here or not?
// close();
}
}

// end

I realize that from a /documentation/ standpoint, I can do either and just
document wether or not the destructor performs cleanup

-- however --

I'm mostly interested in a self-documenting code perspective, where
(external)
documentation isn't referenced.

Comments???

IMO the purpose of a dtor is exactly that: put things back the
way you found them.

-Mike



Back to top
Julie
Guest





PostPosted: Fri Jan 28, 2005 1:47 am    Post subject: Re: Should resource wrappers auto cleanup on destruct? Reply with quote

red floyd wrote:
Quote:
Julie wrote:

(I searched the FAQ, but didn't find anything relevant -- if there is,
please post a link.)
[redacted]


Google for "RAII" (Resource Acquisition Is Initialization).


Thanks.

Back to top
Jonathan Turkanis
Guest





PostPosted: Fri Jan 28, 2005 2:20 am    Post subject: Re: Should resource wrappers auto cleanup on destruct? Reply with quote

Mike Wahler wrote:

Quote:
IMO the purpose of a dtor is exactly that: put things back the
way you found them.

This is a rather Sisyphisian view of class design, isn't it? Won't clients start
complaining that no work is getting done? :-)

Quote:
-Mike

Jonathan



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.