While browsing the net in search for a safe, easily deallocable singleton I've come upon the solution I'm posting below. One nice thing about it is that the singleton instance is not allocated on the heap but instead declared as a static variable inside of the instance method, which guarantees both the automatic initialization when the method is first called and easy cleanup once the program terminates. Also, the copy constructor and the "=" operator are both declared as private and overloaded so as to prevent the instance to be copied, but that should be obvious. Decided to share.
I hope it's not a repost.
class cSingleton {
public:
static cSingleton* instance() {
static cSingleton ptr;
return &ptr;
}
private:
~cSingleton() { }
cSingleton() { }
cSingleton(const cObjectManager& inst) {}
void operator=(const cSingleton&) const {}
};












