Re: Multiple destructors?
- From: "Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 10 Jan 2006 18:41:45 +0100
"Tom de Neef" <tdeneef@xxxxxxxx> wrote in message
news:43c3ba9d$0$11076$e4fe514c@xxxxxxxxxxxxxxxxx
> "Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx> schreef in bericht
> news:43c2dfd8$0$11065$e4fe514c@xxxxxxxxxxxxxxxxx
>> Has anyone here ever written a class with more than one destructor?
>> I was just toying with some hobby code where objects are used to model
>> a filesystem, and wondering how I might add the feature of deleting
>> files and directories[0]. There are filesystem, file, and directory
>> objects, and it seemed to me that destroying a file object might be a
>> very natural way to also delete the corresponding file.
>> [...]
> I remember an application where I had called destructors in a derived
> class of Tlist DONE instead of DESTROY.
> Destroy would just destroy the list, but leave its elements intact.
> Done would first free all elements and then free the list.
That's like TObjectList, which may or may not own the objects in it,
only different. The similarity to ownership schemes had not occurred
to me.
TObjectList has a property OwnsObjects. I could have a property in my
file objects, but it would just be waiting to be used as an implicit
parameter to Destroy. I don't know what's worse: static parameters or
parameters on destructors.
> But at one
> place I messed it up because I hadn't realized that Destroy has a
> special meaning. For it is the destructor called by Free. And if you
> are in the habit of calling Free you may have memory leaks when there
> isn't a Destroy in the class.
Every class should have a Destroy destructor. I noticed today that it
even has its own slot in the negative offset section of the VMT.
Actually, on checking, all virtual methods in TObject are in the
negative offsets. Simply a matter of where it was decided for them
that they should start counting, apparently.
Groetjes,
Maarten Wiltink
.
- References:
- Multiple destructors?
- From: Maarten Wiltink
- Re: Multiple destructors?
- From: Tom de Neef
- Multiple destructors?
- Prev by Date: Re: Help reducing object instance size
- Next by Date: Re: Help reducing object instance size
- Previous by thread: Re: Multiple destructors?
- Next by thread: Help reducing object instance size
- Index(es):
Relevant Pages
|