Design Patterns in C++: Singleton

Design Patterns in C++: Singleton

The Singleton Design Pattern guarantees that there is always at most one lonesome instance of the class around.

The following implementation of a Singleton in CPP is thread-safe as long as you are using C++11 or above! It is based on a stackoverflow Q&A.


class Singleton_ClassName {

	Singleton_ClassName* bc_singleton;


	static Singleton_ClassName& getInst();

	// We need to delete the copy and move constructors
	// and, furthermore, the assign operators
	Singleton_ClassName(Singleton_ClassName const&) = delete;             // copy constructor
	Singleton_ClassName(Singleton_ClassName&&) = delete;                  // move constructor
	Singleton_ClassName& operator=(Singleton_ClassName const&) = delete;  // copy assignment
	Singleton_ClassName& operator=(Singleton_ClassName &&) = delete;      // cove assignment


Singleton_ClassName& Singleton_ClassName::getInst()
	static BrickChecker instance();
	return instance;

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email