Re: private destructor

From: lilburne (lilburne_at_godzilla.net)
Date: 11/18/03


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?



Relevant Pages

  • Re: private destructor
    ... > class MyClass ... > unless you want to use a singleton DP ... A private destructor is a perfectly fine thing to have. ... A work around for the warning is to create a friend and never ...
    (comp.lang.cpp)
  • Re: private destructor
    ... > The purpose of the private destructor is to catch any attempts to ... > I can of course add a dummy friend, ... class MyClass ... unless you want to use a singleton DP ...
    (comp.lang.cpp)
  • private destructor
    ... The purpose of the private destructor is to catch any attempts to ... I can of course add a dummy friend, ...
    (comp.lang.cpp)