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
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