Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Singleton question

Status
Not open for further replies.

abp

Programmer
Sep 14, 2000
134
0
0
FR
Hi

Is it legal to declare a destructor for a class that follows the 'Singleton' pattern ?

I mean is there anything inherent in the pattern that prevents or rather discourages the programmer to prevent declaring a destructor for the singleton class...

Thanks

Anand pillai ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
There is nothing that prevents you from using a destructor for the non-static data but the Singleton class usually has a destroy method that will delete the single instance of the class. There is also a method that is a Get to return the instance of the class. Both are static. Also, the constructor and destructor are protected or private.

Matt
 

THanks for replying. I guess that you mean
something like below ?

Singleton::Instance()
{
if (pinstance==NULL)
pinstance = new Singleton();

return pinstance;
}

Singleton::Singleton()
{
// whatever you want to do with the members
}

Singleton::~Singleton()
{
if (pinstance)
delete pinstance;

pinstance=NULL;
}


I read somewhere that the OS should manage the destruction of the singleton instance and you *SHOULD* not write code
like what I have sampled above. That is you should not delete the instance of the singleton inside your destructor,
though the compiler will allow it.

Anand Pillai ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Right... it will allow it. Here is a reason why. Suppose you derive a class from the singleton class called superSingleton but superSingleton has a public constructor. In the private section of the class you declare an object of type Singleton. When superSingleton goes out of scope it calls the destructor of Singleton. Not too super any more. Anyone who can declare a type of class singleton can run the risk of unintentionally deleting it. That is why the Destroy method is suggested. You manage yourself when it will be deleted.

Matt
 
Ummm... i meant "here is a reason why to NOT do it"

:p

Matt
 
Hi Zyrenthian

Thanks for the replies.

Anand ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top