Re: private destructor
From: lilburne (lilburne_at_godzilla.net)
Date: 11/18/03
- Next message: Mike Wahler: "Re: iostreams equivalent to C's fopen "r+""
- Previous message: Rick Noelle: "Re: iostreams equivalent to C's fopen "r+""
- In reply to: Arve Sollie: "private destructor"
- Next in thread: .oO LGV Oo.: "Re: private destructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 18 Nov 2003 22:43:53 +0000
Arve Sollie wrote:
> class myClass
> {
> private:
> int refCount;
> ~myClass();
>
> public:
> myClass();
>
> void incRefCount() { ++refCount; }
> void decRefCount() { if (--refCount <=0) delete this; }
> };
>
> The purpose of the private destructor is to catch any attempts to
> delete the object while still referenced, but my compiler warns me
> that I have only private destructors and no friends.
> I can of course add a dummy friend, but is this really neccessary ?
>
>
With this you have to ensure that someone doesn't either do
inRefCount() twice, or decRefCount() twice, or forget to
incRefCount() or forget to decRefCount(). You're accepting
those problems for a concern over deleting something that is
still referenced?
Why complicate matters? Why not use reference counting smart
pointers?
- Next message: Mike Wahler: "Re: iostreams equivalent to C's fopen "r+""
- Previous message: Rick Noelle: "Re: iostreams equivalent to C's fopen "r+""
- In reply to: Arve Sollie: "private destructor"
- Next in thread: .oO LGV Oo.: "Re: private destructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|