 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Julie Guest
|
Posted: Thu Jan 27, 2005 11:42 pm Post subject: Should resource wrappers auto cleanup on destruct? |
|
|
(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
|
Posted: Fri Jan 28, 2005 12:11 am Post subject: Re: Should resource wrappers auto cleanup on destruct? |
|
|
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
|
Posted: Fri Jan 28, 2005 12:26 am Post subject: Re: Should resource wrappers auto cleanup on destruct? |
|
|
"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
|
Posted: Fri Jan 28, 2005 1:47 am Post subject: Re: Should resource wrappers auto cleanup on destruct? |
|
|
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
|
Posted: Fri Jan 28, 2005 2:20 am Post subject: Re: Should resource wrappers auto cleanup on destruct? |
|
|
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? :-)
Jonathan
|
|
| Back to top |
|
 |
|
|
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
|
|