Safe Singleton (C++)http://forum.devmaster.net/t/safe-singleton-c/17962
Hello, sorry for bothering.
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 {}
};
Fri, 23 Apr 2010 12:56:48 +0000languagesSafe Singleton (C++)@JarkkoL wrote:

Well, what I have heard, OGRE isn't exactly a prime example of good software engineering practices and is often referred as being over-engineered with excessive inheritance hierarchies, etc.

I have never found any practical use for singletons. Probably the closest altertative I use is activation of a class instance globally and accessing the instance through a global accessor. Upon destruction of an instance check if it's active and deactivate if so.

Factory Design Patterns are useful. They are often a singleton as you normally only want one factory, with global availability, through a single access point.

I have never found any practical use for singletons. Probably the closest altertative I use is activation of a class instance globally and accessing the instance through a global accessor. Upon destruction of an instance check if it's active and deactivate if so.

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.