473,324 Members | 2,179 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,324 software developers and data experts.

Private Destructor - Use

Hi
What is the use declaring a dtor as private?
In one of the design tips on net, it is said that this prevents
possibiltiy of creating automatic variable of the class. Is this true?
Can this concept work in a Singleton?
How memory leak can be avoided since delete can not be called because
dtor is private?

Thanks in advance
Amol

Jun 7 '07 #1
2 1700
On 7 Juni, 07:51, Amol <Amol.Ga...@gmail.comwrote:
Hi
What is the use declaring a dtor as private?
In one of the design tips on net, it is said that this prevents
possibiltiy of creating automatic variable of the class. Is this true?
Yes
Can this concept work in a Singleton?
Yes
How memory leak can be avoided since delete can not be called
because dtor is private?
You have to create a special function that deletes instances of the
class, this can either be a static function or a member function. If I
were to use this I would make the constructor private too.

class Test
{
int i_;
~Test() {}
Test(int i) : i_(i) {}
public:
static Test* create(int i)
{
return new Test(i);
}
static void del(Test* t)
{
delete t;
}
};

int main()
{
Test* t1 = Test::create(4);
Test::del(t1);

Test* t2 = new Test(4); // Does not work
Test t3(4); // Does not work either
}

--
Erik Wikström

Jun 7 '07 #2
study...

Jun 7 '07 #3

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...
3
by: Arve Sollie | last post by:
class myClass { private: int refCount; ~myClass(); public: myClass(); void incRefCount() { ++refCount; }
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.