By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,401 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

Destructor in Singleton class

P: 12
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
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
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

P: 12
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
Expert 100+
P: 542
"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
Expert Mod 5K+
P: 8,916
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
Expert Mod 5K+
P: 9,197
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

Post your reply

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