Re: private destructor
From: .oO LGV Oo. (_NOSPAM_legeantvert_at_tiscali.fr)
Date: 11/18/03
- Next message: Luc The Perverse: "Re: Manic C++ programming"
- Previous message: Fred Ma: "Manic C++ programming"
- In reply to: Arve Sollie: "private destructor"
- Next in thread: Gianni Mariani: "Re: private destructor"
- Reply: Gianni Mariani: "Re: private destructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Luc The Perverse: "Re: Manic C++ programming"
- Previous message: Fred Ma: "Manic C++ programming"
- In reply to: Arve Sollie: "private destructor"
- Next in thread: Gianni Mariani: "Re: private destructor"
- Reply: Gianni Mariani: "Re: private destructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|