473,320 Members | 1,955 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,320 software developers and data experts.

Destructor in Singleton class

1. What should be the access specifier for the destructor function in a singleton class?
private, public or protected.

2. How should destroying the object of the singleton be handled?
Sep 6 '10 #1
5 9697
Banfa
9,065 Expert Mod 8TB
In my company we make the destructor protected and then provide a static public function to delete the singleton object, the in same way that you normally provide a static public function to allocate and gain access to the object.
Sep 6 '10 #2
But in that case how would you make sure that when this static public function containing destructor function is called, no objects of the singleton class would be using the singleton class?
Sep 6 '10 #3
ashitpro
542 Expert 512MB
"objects of the singleton class" ??

I guess, we always refer to only one object. Ain't we?

"static public function containing destructor function" ??

What is this ?? Please, elaborate !! Just not getting you
Sep 6 '10 #4
Banfa
9,065 Expert Mod 8TB
Here this is the basic structure we use, obviously with added member data and methods

Expand|Select|Wrap|Line Numbers
  1. // Header
  2. class Singleton
  3. {
  4. public:
  5.     /**
  6.      * definition of public member function here
  7.      */
  8.  
  9.     /**
  10.      * static method to return the single instance of this class
  11.      */
  12.     static Singleton* getInstance();
  13.  
  14.     /**
  15.      * static method to delete the single instance of this class
  16.      */
  17.     static void deleteInstance();
  18.  
  19. protected:
  20.     /**
  21.      * Member data here
  22.      */
  23.  
  24.     /**
  25.      * This is the pointer to the single instance of this class
  26.      */
  27.     static Singleton* m_sInstance;
  28.  
  29.     /**
  30.      * Protected constructor prevents outside entities creating us
  31.      */
  32.     Singleton();
  33.  
  34.     /**
  35.      * Protected destructor prevents outside entities deleting us
  36.      */
  37.     ~Singleton();
  38. };
  39.  
Expand|Select|Wrap|Line Numbers
  1. // Source File
  2.  
  3. /**
  4.  * This is the pointer to the single instance of this class
  5.  */
  6. Singleton* Singleton::m_sInstance = 0;
  7.  
  8. /**
  9.  * Returns the instance of the singleton
  10.  */
  11. Singleton* Singleton::getInstance()
  12. {
  13.     // If no instance has been created yet create the instance first
  14.     if (m_sInstance == 0)
  15.     {
  16.         m_sInstance = new Singleton;
  17.     }
  18.  
  19.     return m_sInstance;
  20. }
  21.  
  22. /**
  23.  * This static function deletes the current instance of the singleton
  24.  */
  25. void Singleton::deleteInstance()
  26. {
  27.     delete m_sInstance;
  28.     m_sInstance = 0;
  29. }
  30.  
  31. Singleton::Singleton()
  32. {
  33. }
  34.  
  35. Singleton::~Singleton()
  36. {
  37. }
  38.  
You have to explicitly call the Singleton::deleteInstance in the same way you have to explicitly call delete for a class instance you have created using new.
Sep 6 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
But in that case how would you make sure that when this static public function containing destructor function is called, no objects of the singleton class would be using the singleton class?
There's no way to tell when it's safe.

Therefore, you should not be using pointers to a Singleton that can be deleted by someone. You need to use a handle (aka a smart pointer) instead. Read this: http://bytes.com/topic/c/insights/65...-smart-pointer
Sep 6 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Robert W. | last post by:
I've just been reading all about the Singleton class and understand how to implement and use it but I cannot understand why one NEEDS to use it instead of just declaring a class and implementing...
15
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
14
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is...
9
by: Marcel Hug | last post by:
Hallo NG ! I Have a little question about inheritance of a singleton class. In my application i have a Database-Connection Lib, in which I would ¨like to connect different databases of the same...
5
by: ernaveenkoul | last post by:
hi, Hope i get some answers. What is single ton class?how to make singleton class?use of single ton class. Regards Naveen
2
by: baba | last post by:
Hi all, I'm quite new to C#. I am trying to implement some basics reusable classes using this language and the .NET Framework technology. What I'm trying to do now is to implement a singleton...
3
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
3
by: gzeng | last post by:
I saw a piece of code from a website. It seems to be a simple example for a singleton class. Basically, the author creates an object in the definition of a class, which has the same name as the...
2
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define...
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
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.