By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

private destructor

P: n/a
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 ?

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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?

Jul 22 '05 #2

P: n/a

"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.
Jul 22 '05 #3

P: n/a
..oO LGV Oo. wrote:
"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.


Um ... nice try but no cigar.

By the time ~MyClass() gets to if (--refCount == 0), a whole bunch of
other destructors may be called (not to mention the ones after
~myClass() is called .

A private destructor is a perfectly fine thing to have.

A work around for the warning is to create a friend and never
instantiate it ?

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.