Template C++ de Singleton con atexit
?Ver código CPP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #ifndef __SINGLETON_ATEXIT__ #define __SINGLETON_ATEXIT__ #include "Uncopyable.h" #include <cstdlib> template<typename T> class Singleton: public uncopyable { public: static T* getInstance(){ if (__instance == NULL){ __instance = new T; std::atexit(destroy); } return __instance; } private: static void destroy(){ delete __instance; } static T* __instance; }; template<typename T> T* Singleton<T>::__instance = NULL; #endif |
?Ver código CPP
1 2 3 4 5 6 7 8 9 10 11 12 13 | #ifndef UNCOPYABLE_H_ #define UNCOPYABLE_H_ class uncopyable{ // Idea from Effictive C++ by Scott Meyers protected: uncopyable() {} ~uncopyable() {} private: uncopyable(const uncopyable &); uncopyable & operator=(const uncopyable &); }; #endif |
| Imprimir artículo | Este artículo fue publicado por brue el octubre 11, 2010 a las 05:28, y está archivado en C++, Programación. Sigue las respuestas a esta entrada a través de RSS 2.0. Puedes dejar un comentario o enviar un trackback desde tu propio sitio. |
![[bRue.org] [bRue.org]](http://www.brue.org/wp-content/uploads/logo_bw.png)