 // Purpose.  Singleton (Scott Meyers    // New design.  "globalObj" is now a
 //                      approach)       // static variable in the inst() ac-
 // Discussion.  On the left, a global   // cessor method.  The single inst-
 // object is architected to require     // ance is enforced by declaring the
 // lazy initialization (not inited un-  // ctor non-public.  [The dtor must
 // til it is needed).  This requires    // be public because of the static
 // all users of the object to test and  // variable instance.]  Global
 // potentially allocate the pointer.    // access is provided by the static
 // Singleton suggests making the class  // inst() method.  The object is al-
 // itself responsible for creating,     // located on first demand by C++,
 // maintaining, and providing global    // and it is de-allocated automati-
 // access to its own single instance.   // cally by C++.

 #include <iostream.h>                   #include <iostream.h>

 class GlobalClass {                     class GlobalClass {
 public:                                 public:
    GlobalClass( int v=0 ) {                int  getValue() {
       value_ = v; }                           return value_; }
    int  getValue() {                       void setValue( int v ) {
       return value_; }                        value_ = v; }
    void setValue( int v ) {                static GlobalClass& inst() {
       value_ = v; }                           static GlobalClass globalObj;
 private:                                      return globalObj; }
    int  value_;                            ~GlobalClass() {
 };                                            cout << ":dtor:" << endl; }
                                         protected:
 // Initializing a global ptr to class      GlobalClass( int v=0 ) {
 // GlobalClass                                cout << ":ctor: ";
 GlobalClass*   globalObj = 0;                 value_ = v; }
                                         private:
 void foo( void )                           int  value_;
 {                                       };
    if ( ! globalObj )
       globalObj = new GlobalClass;      void foo( void )
    globalObj->setValue( 1 );            {
    cout << "foo: globalObj is " <<         GlobalClass::inst().setValue( 1 );
       globalObj->getValue() << endl;       cout << "foo: globalObj is " <<
 }                                             GlobalClass::inst().getValue()
                                               << endl;
 void bar( void )                        }
 {
    if ( ! globalObj )                   void bar( void )
       globalObj = new GlobalClass;      {
    globalObj->setValue( 2 );               GlobalClass::inst().setValue( 2 );
    cout << "bar: globalObj is " <<         cout << "bar: globalObj is " <<
       globalObj->getValue() << endl;          GlobalClass::inst().getValue()
 }                                             << endl;
                                         }
 void main( void )
 {                                       void main( void )
    if ( ! globalObj )                   {
       globalObj = new GlobalClass;         cout << "main: globalObj is " <<
    cout << "main: globalObj is " <<           GlobalClass::inst().getValue()
       globalObj->getValue() << endl;          << endl;
    foo();                                  foo();
    bar();                                  bar();
 }                                          cout << "main: end" << endl;
                                         }
 // main: globalObj is 0
 // foo: globalObj is 1                  // main: globalObj is :ctor: 0
 // bar: globalObj is 2                  // foo: globalObj is 1
                                         // bar: globalObj is 2
                                         // main: end
                                         // :dtor:

