473,654 Members | 3,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8821
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*******@mobi lpost.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*******@mobi lpost.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
21382
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 an idea that we can have private constructors and destructors but am not able to find a situation where they can be used... Regards RVG rajeshgarg@opussoft.com
5
6427
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 std::endl;
6
2561
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 drived; public: base() {cout<<"I am base constructor"<<endl;} virtual void test1() {cout << "i am base virtual method " <<endl;}
23
2588
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. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
6
2195
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 constructor as private and destructor as public. class Single { private:
7
4282
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
2630
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 Add_Molecule_Req: public Action_Request{ // ...... };
0
8285
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8706
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7304
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.