singleton.h

Go to the documentation of this file.
00001 
00014 #ifndef _TEMPLATES__SINGLETON_H_
00015 #define _TEMPLATES__SINGLETON_H_
00016 
00020 namespace Templates
00021 {
00022         template <class T>
00023         class Singleton;
00024 }
00025 
00032 template <class T>
00033 class Templates::Singleton
00034 {
00035         private:
00036 
00037                 // Disable both constructors, destructor and assignment operator
00038                 Singleton();
00039                 ~Singleton();
00040                 Singleton(const Singleton &);
00041                 Singleton & operator=(const Singleton &);
00042 
00043         public:
00044 
00050                 static T & instance()
00051                 {
00052                         // This variant of returning static variable has a big advantage -
00053                         // you don't have to care about destruction of the holding object
00054                         // (if it was using dynamic allocation or it was declared static
00055                         // as a private variable).
00056                         // Sometimes it's called "Meyer's singleton", according to the author.
00057                         static T instance;
00058                         return instance;
00059                 }
00060 };
00061 
00062 #endif

Generated on Sat Dec 20 19:21:24 2008 for PGR2008 by  doxygen 1.5.6