#include <stdio.h>
class Singleton {
private:
int* m_pi ;
protected:
Singleton(Singleton& s){}
Singleton& operator=(Singleton& s){return *this;}
Singleton(){
printf("Singleton コンストラクト\n") ;
m_pi = new int(0) ;
}
public:
~Singleton(){
delete m_pi ;
m_pi = 0 ;
printf("Singleton デストラクト\n") ;
}
static Singleton* Instance() ;
int i(void) const {return *m_pi ;}
int i(int x) {*m_pi = x ; return x ;}
} ;
Singleton* Singleton::Instance() {
static Singleton _instance ;
return &_instance ;
}
static void set(int i){
Singleton::Instance()->i(i) ;
}
static int get(void){
return Singleton::Instance()->i() ;
}
int main(void){
set(10) ;
printf("%d\n", get()) ;
delete Singleton::Instance() ;
try{
printf("%d\n", get()) ;
}catch(...){
printf("bad Singleton::_instance.\n") ;
}
return 0 ;
}