 // Purpose.  Chain of Responsibility.   #include <iostream.h>
 //
 // Discussion.  Instead of the client   class H {
 // having to know about the number of   public:
 // "handlers", and manually map re-        H( H* next = 0 ) {
 // quests to available handlers; de-          id_   = count_++;
 // sign the handlers into an "intel-          busy_ = 0;
 // ligent" chain.  Clients "launch            next_ = next;
 // and leave" requests with the head       }
 // of the chain.                           ~H() {
                                               cout << id_<<" dtor" << endl;
 #include <iostream.h>                         // don't get into a loop
                                               if (next_->next_) {
 class H {                                        H* t = next_;
 public:                                          next_ = 0;
    H() {                                         delete t;
       id_ = count_++;                      }  }
       busy_ = 0;                           void setNext( H* next ) {
    }                                          next_ = next;
    ~H() {                                  }
       cout << id_ << " dtor" << endl;      void handle() {
    }                                          if (busy_ = ! busy_)
    int handle() {                                cout << id_ << " handles"
       if (busy_ = ! busy_) {                        << endl;
          cout << id_ << " handles"            else {
             << endl;                             cout << id_ << " is busy"
          return 1;                                  << endl;
       } else {                                   next_->handle();
          cout << id_ << " is busy"         }  }
             << endl;                    private:
          return 0;                         int        id_, busy_;
    }  }                                    H*         next_;
 private:                                   static int count_;
    int        id_, busy_;               };
    static int count_;                   int H::count_ = 1;
 };
 int H::count_ = 1;                      H* setUpList() {
                                            H* first = new H;
 void main( void ) {                        H* last = new H(first);
    const int TOTAL = 2;                    first->setNext( last );
    H* list[TOTAL] = { new H, new H };      return first;
                                         }
    for (int i=0; i < 6; i++)
       for (int j=0; 1 ;                 void main( void ) {
                j = (j + 1) % TOTAL)        H* head = setUpList();
          if (list[j]->handle())
             break;                         for (int i=0; i < 6; i++)
                                               head->handle();
    for (int k=0; k < TOTAL; k++)
       delete list[k];                      delete head;
 }                                       }

 // 1 handles                            // 1 handles
 // 1 is busy                            // 1 is busy
 // 2 handles                            // 2 handles
 // 1 handles                            // 1 handles
 // 1 is busy                            // 1 is busy
 // 2 is busy                            // 2 is busy
 // 1 handles                            // 1 handles
 // 1 is busy                            // 1 is busy
 // 2 handles                            // 2 handles
 // 1 handles                            // 1 handles
 // 1 dtor                               // 1 dtor
 // 2 dtor                               // 2 dtor

