c++代理类代码示例


欢迎转载请注明出处:海漩涡

http://blog.csdn.net/tanhuifang520


C++的代理类

作用:包含类型不同而彼此相关的对象


例如,将不同的类对象放入同一个代理类的数组中。



#include 
using namespace std;

class A{
	public:
		virtual A* copy() = 0;
		virtual void show() = 0;
};

class B:public A{
	public:
		virtual B* copy() { return new B(*this); }
		virtual void show() { cout<<"I am class B"<copy()){}
		surrogate &operator=(const surrogate &s)
		{
			if(this != &s)
			{
				delete this->Pa;
				this->Pa = s.Pa;
			}
			return *this;
		}
		void show(){ Pa->show(); }
	private:
		A *Pa;
};
int main()
{
	surrogate S[10];
	B b;
	S[0] = b;
	S[0].show();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/tanhuifang520/article/details/63252857
今日推荐