private destructor

A

Arve Sollie

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 ?
 
L

lilburne

Arve said:
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?
 
O

.oO LGV Oo.

Arve Sollie said:
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.
 
G

Gianni Mariani

..oO LGV Oo. said:
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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,375
Latest member
FelishaCma

Latest Threads

Top