C++ 恢复访问权限示例

Prototype模式通过复制原型(Prototype)而获得新对象创建的功能,这里Prototype本身就是“对象工厂”

prototype.h:

#ifndef _PROTOTYPE_H_
#define _PROTOTYPE_H_

class Prototype
{
public:
	virtual ~Prototype();
	virtual Prototype* Clone() const = 0;
protected:
	Prototype();
private:
};

class ConcretePrototype:public Prototype
{
public:
	ConcretePrototype();
	ConcretePrototype(const ConcretePrototype& cp);
	~ConcretePrototype();
	Prototype* Clone() const;
protected:
private:
};
#endif //~_PROTOTYPE_H_
prototype.cpp:

#include "prototype.h"
#include <iostream>
using namespace std;


Prototype::Prototype(){

}
Prototype::~Prototype(){

}
Prototype* Prototype::Clone() const{

	return 0;
}


ConcretePrototype::ConcretePrototype(){

}
ConcretePrototype::~ConcretePrototype(){

}
ConcretePrototype::ConcretePrototype(const ConcretePrototype& cp){

	cout<<"ConcretePrototype copy ..."<<endl;
}
Prototype* ConcretePrototype::Clone() const{

	return new ConcretePrototype(*this);
}
main.cpp:

#include "prototype.h"

#include <iostream>
using namespace std;


int main(){

	Prototype* p = new ConcretePrototype();
	Prototype* p1 = p->Clone();

	return 1;
}




C++ 恢复访问权限示例,布布扣,bubuko.com

C++ 恢复访问权限示例

上一篇:C++编码实践-1 成为职业软件人


下一篇:World!编程语言设计理念