Singleton Pattern in C++

Most contents are selected from http://www.codeproject.com/Articles/1921/Singleton-Pattern-its-implementation-with-C

Suppose we have to use a single object of class throughout the lifetime of an application. in C++, we can declare a global object. Or use a more practical solution, which is to get a single object by declaring a class, which contains only static methods, and this is singleton design pattern. But remember that a class with only static methods and variables are not a good object oriented design.

The Singletons are often used to control access to resources such as database connections or sockets. ♠

By using a private constructor and a static method to create and return an instance of the class is a popular way for implementing Singleton Pattern.

 

Advertisements