"Arve Sollie" <co*******@mobilpost.com> a écrit dans le message de
news:3f********@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.