private destructor

From: Arve Sollie (codeworks_at_mobilpost.com)
Date: 11/18/03


Date: Tue, 18 Nov 2003 23:06:05 +0100

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 ?



Relevant Pages

  • 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)
  • 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 ... incRefCountor forget to decRefCount(). ... Why not use reference counting smart ...
    (comp.lang.cpp)