#include <singleton.hpp>
Inheritance diagram for Singleton< T >:
Public Member Functions | |
Singleton () | |
Default constructor. | |
~Singleton () | |
Default destructor. | |
Static Public Member Functions | |
T & | getSingleton () |
Provides access to instance. | |
T * | getSingletonPtr () |
Provides access to instance. |
A singleton ensures that only one instance of a class exists at one time. A class is converted to a singleton by deriving it from Singleton and giving the user's class as template parameter.
However unlike most other implementations the instance must be created by the user. It Is not created automatically. It has to be deleted by the user, too. See the example for details.
Access is provided by getSingleton() (returns a reference) or by getSingletonPtr() (returns a pointer).
Now a simple example of the usage:
#include <singleton.hpp> #include <iostream> using namespace std; class Hello : pulbic Singleton<Hello> { public: sayHello() {cout << "Hello\n"}; }; int main() { // create singleton new Hello(); // say hello Hello::getSingleton().sayHello(); Hello::getSingletonPtr()->sayHello(); // clean up delete Hello::getSingletonPtr(); return 0; }
Definition at line 39 of file singleton.hpp.
|
Default constructor. Definition at line 47 of file singleton.hpp. |
|
Default destructor. Definition at line 58 of file singleton.hpp. |
|
Provides access to instance.
|
|
Provides access to instance.
|