代理类

来源:互联网 发布:电影源码带采集 编辑:程序博客网 时间:2024/05/22 03:49

Implementation.h

class Implementation{public:Implementation(int v)  :value(v){}void setValue(int v){value=v; }int getValue() const{ return value; } private:int value;};

Interface.h

class Interface{public:Interface(int);void setValue(int);int getValue() const;~Interface(); private:Implementation *ptr;};

Interface.cpp

#include“Implementation.h”Interface::interface(int v)  :ptr(new Imlementation(v)){}void Interface::setValue(int v){ptr->setValue(v);}int Interface::getValue() const{return ptr->getValue();}Interface::~Interface(){delete ptr;}
demo.cpp

#include<iostream>using namespace std;#incldue"INterface.h"int main(){Interface i(5); cout<<i.getValue(); i.setValue(10); cout<<i.getValue()<<endl; return 0;}
结果:
5
10


0 0
原创粉丝点击