Re: private destructor

From: .oO LGV Oo. (_NOSPAM_legeantvert_at_tiscali.fr)
Date: 11/18/03


Date: Tue, 18 Nov 2003 23:52:28 +0100


"Arve Sollie" <codeworks@mobilpost.com> a écrit dans le message de
news:3fba97cf$1@news1e1.seinf.abb.se...
> 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 ?

maybe an alternative way to do the same without warnings :

class MyClass
{
public:
    MyClass() { refCount++; }
    ~MyClass() { if (--refCount == 0) { this->destroy(); } }

private:
    static Data *m_pData; // whatever data to be shared by all instances,
unless you want to use a singleton DP
    void destroy() { /* cleanup m_pData */ }
};

this way, all instances can be destroyed but the shared data is only
released when there's no instance anymore... :-?
but I think maybe you should use a singleton DP with the same count as in
this example.



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