C++责任链

#include <iostream>
using namespace std;
class Base{
    protected:
        Base *next;
        public:
            Base(Base *t){
                next=t;
            }
            Base(){
            next=NULL;
            }
            virtual void fun(){
            if(next)    next->fun();
            }
            virtual ~Base(){}
};
class A1:public Base{
    public:
        A1(Base *t):Base(t){
        }
        A1(){
        }
        void fun(){
            cout<<"a1"<<endl;
            Base::fun();
        }
};

class A2:public Base{
    public:
        A2(Base *t):Base(t){
        };
        A2(){
        }
        void fun(){
            cout<<"a2"<<endl;
            Base::fun();
        }
};

class A3:public Base{
    public:
        A3(Base *t):Base(t){

        }
        A3(){
        }
        void fun(){
            cout<<"a3"<<endl;
            Base::fun();
        }
};
int main(void)
{
    A3 *pb3=(A3 *)new A3();
    A2 *pb2=(A2 *)new A2(pb3);
    A1 *pb1=(A1 *)new A1(pb2);
    pb1->fun();
    return 0;
}
posted @ 2015-03-08 23:17  机智的程序员小熊  阅读(174)  评论(0编辑  收藏  举报