 // Purpose.  Singleton                  // New design.  "globalObj" is now a
 //                                      // private static data member of its
 // Discussion.  On the left, a global   // own class.  Global access is pro-
 // object is architected to require     // vided by the public static member
 // lazy initialization (not inited un-  // function inst().  And the lazy
 // til it is needed).  This requires    // initialization code is encapsu-
 // all users of the object to test and  // lated in the inst() function.
 // potentially allocate the pointer.    // GlobalClass's ctor and dtor have
 // Singleton suggests making the class  // been made protected so that cli-
 // itself responsible for creating,     // ents cannot create more inst's
 // maintaining, and providing global    // or destroy the Singleton inst.
 // access to its own single instance.
                                         class GlobalClass {
 #include <iostream.h>                   public:
                                            int  getValue() {
 class GlobalClass {                           return value_; }
 public:                                    void setValue( int v ) {
    GlobalClass( int v=0 ) {                   value_ = v; }
       value_ = v; }                        static GlobalClass* inst() {
    int  getValue() {                          if ( ! globalObj_ )
       return value_; }                           globalObj_ = new GlobalClass;
    void setValue( int v ) {                   return globalObj_; }
       value_ = v; }                     protected:
 private:                                   GlobalClass( int v=0 ) {
    int  value_;                               value_ = v; }
 };                                         ~GlobalClass() { }
                                         private:
 // Initializing a global ptr to class      int    value_;
 // GlobalClass                             static GlobalClass* globalObj_;
 GlobalClass*   globalObj = 0;           };

 void foo( void )                        // Allocating and initializing
 {                                       // GlobalClass's static data member
    if ( ! globalObj )                   // (the ptr, not a GlobalClass inst)
       globalObj = new GlobalClass;      GlobalClass*
    globalObj->setValue( 1 );               GlobalClass::globalObj_ = 0;
    cout << "foo: globalObj is " <<
       globalObj->getValue() << endl;    void foo( void )
 }                                       {
                                            GlobalClass::inst()->setValue( 1 );
 void bar( void )                           cout << "foo: globalObj is " <<
 {                                             GlobalClass::inst()->getValue()
    if ( ! globalObj )                         << endl;
       globalObj = new GlobalClass;      }
    globalObj->setValue( 2 );
    cout << "bar: globalObj is " <<      void bar( void )
       globalObj->getValue() << endl;    {
 }                                          GlobalClass::inst()->setValue( 2 );
                                            cout << "bar: globalObj is " <<
 void main( void )                             GlobalClass::inst()->getValue()
 {                                             << endl;
    if ( ! globalObj )                   }
       globalObj = new GlobalClass;
    cout << "main: globalObj is " <<     void main( void )
       globalObj->getValue() << endl;    {
    foo();                                  cout << "main: globalObj is " <<
    bar();                                     GlobalClass::inst()->getValue()
 }                                             << endl;
                                            foo();
 // main: globalObj is 0                    bar();
 // foo: globalObj is 1                  }
 // bar: globalObj is 2
                                         // main: globalObj is 0
                                         // foo: globalObj is 1
                                         // bar: globalObj is 2

