 // Purpose.  Singleton destroyer        class GlobalClass;
 //
 // Discussion.  Vlissides describes     class SingDest {
 // that Singletons can be cleaned-up    public:
 // by "wrapping" the ptr in a stack-       SingDest( GlobalClass* s=0 ) {
 // based static member of another             sing_ = s; }
 // class whose sole responsibility is      ~SingDest();
 // to have its destructor delete the       void setSing( GlobalClass* s ) {
 // Singleton's ptr.  The Singleton            sing_ = s; }
 // destroyer is automatically cre-      private:
 // ated before main() is run, and          GlobalClass*  sing_;
 // initially contains a null ptr.       };
 // The first time the inst() method
 // is called, the destroyer is          class GlobalClass {
 // meaningfully initialized.            public:
                                            friend class SingDest;
 #include <iostream.h>                      int  getValue() { return value_; }
                                            void setValue( int v ) {
 class GlobalClass {                           value_ = v; }
 public:                                    static GlobalClass* inst() {
    int  getValue() {                          if ( ! globalObj_ ) {
       return value_; }                           globalObj_ = new GlobalClass;
    void setValue( int v ) {                      dest_.setSing( globalObj_ ); }
       value_ = v; }                           return globalObj_; }
    static GlobalClass* inst() {         private:
       if ( ! globalObj_ )                  GlobalClass( int v=0 ) {
          globalObj_ = new GlobalClass;        cout << ":ctor: ";
       return globalObj_; }                    value_ = v; }
 protected:                                 ~GlobalClass() {
    GlobalClass( int v=0 ) {                   cout << ":dtor:" << endl; }
       value_ = v; }                        int    value_;
    ~GlobalClass() { }                      static GlobalClass* globalObj_;
 private:                                   static SingDest dest_;
    int    value_;                       };
    static GlobalClass* globalObj_;
 };                                      GlobalClass*
                                            GlobalClass::globalObj_ = 0;
 // Allocating and initializing          SingDest GlobalClass::dest_;
 // GlobalClass's static data member     SingDest::~SingDest() { delete sing_; }
 // (the ptr, not a GlobalClass inst)
 GlobalClass*                            void foo( void ) {
    GlobalClass::globalObj_ = 0;            GlobalClass::inst()->setValue( 1 );
                                            cout << "foo: globalObj is " <<
 void foo( void ) {                            GlobalClass::inst()->getValue()
    GlobalClass::inst()->setValue( 1 );        << endl;
    cout << "foo: globalObj is " <<      }
       GlobalClass::inst()->getValue()   void bar( void ) {
       << endl;                             GlobalClass::inst()->setValue( 2 );
 }                                          cout << "bar: globalObj is " <<
 void bar( void ) {                            GlobalClass::inst()->getValue()
    GlobalClass::inst()->setValue( 2 );        << endl;
    cout << "bar: globalObj is " <<      }
       GlobalClass::inst()->getValue()   void main( void ) {
       << endl;                             cout << "main: globalObj is " <<
 }                                             GlobalClass::inst()->getValue()
 void main( void ) {                           << endl;
    cout << "main: globalObj is " <<        foo();
       GlobalClass::inst()->getValue()      bar();
       << endl;                             cout << "main: end" << endl;
    foo();                               }
    bar();
 }                                       // main: globalObj is :ctor: 0
                                         // foo: globalObj is 1
 // main: globalObj is 0                 // bar: globalObj is 2
 // foo: globalObj is 1                  // main: end
 // bar: globalObj is 2                  // :dtor:

