473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

private destructor

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
3 8808
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

"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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
5
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using...
6
by: pragtideep | last post by:
I am trying to understand the following code kindly help me #include<iostream> using namespace std; class base { private: ~base() {cout << "This is base destructor"<<endl ;} friend class...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
6
by: VSP | last post by:
Hello, I am just implementing singleton pattern in various ways. In one implementation I created a static member and returning that static member in the getInstance() function. I have made...
7
by: Rahul | last post by:
Hi Everyone, I was trying to implement a final class and i start having the destructor of the final class as private, class A { ~A() { printf("destructor invoked\n");
23
by: Chris Gordon-Smith | last post by:
Hello All I have a base class called Action_Request, and a set of classes corresponding to different kinds of Action_Request, each of which inherits from Action_Request. Eg:- class ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.