KhanS KhanS - 1 month ago 4
C++ Question

private destructor for singleton class

Is it compulsory to have a private destructor for a singleton class.


This might not be what you are looking for.. But for reference, I use it as follows:

// .h
class Foo {
    static Foo* getInstance();
    static void destroy();

    static Foo* myInstance;

// .cpp
Foo* Foo::myInstance = NULL;

Foo* Foo::getInstance(){
    if (!myInstance){
        myInstance = new Foo();
    return myInstance;
void Foo::destroy(){
    delete myInstance;
    myInstance = NULL;

Then at the end of my program, I call destroy on the object. As P├ęter points out the system will reclaim the memory when your program ends, so there is no real reason. The reason I use a destroy is when Ogre complained that I hadn't released all the memory I allocated. After that I just use it as "good manner", since I like cleaning up after myself.