标签:c++ 设计模式
#include "stdafx.h" #include<iostream> #include<cstring> using namespace std; class Prototype { public: virtual ~Prototype() { cout << "~Prototype" << endl; } virtual Prototype* clone()const = 0; virtual void show()const = 0; }; class ConcreteProtype1 :public Prototype { public: ConcreteProtype1(int len,char*str) { _length = len; _str = new char[_length]; strcpy_s(_str, _length, str); } ~ConcreteProtype1() { delete(_str); cout << "~ConcreteProtype" << endl; } ConcreteProtype1(const ConcreteProtype1& rhs) { //实现深拷贝 _length = rhs._length; _str = new char[_length]; if (_str != NULL) strcpy_s(_str, _length, rhs._str); cout << "copy construct ConcreteProtype1" << endl; } virtual Prototype* clone()const { return new ConcreteProtype1(*this); } virtual void show()const { cout <<"value:"<< _str << endl; cout << "Address:" << &_str << endl; } private: int _length; char* _str; };
// PrototypePattern.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Prototype.h" int _tmain(int argc, _TCHAR* argv[]) { Prototype* p1 = new ConcreteProtype1(6,"hello"); Prototype *p2 = p1->clone(); p1->show(); p2->show(); getchar(); return 0; }
标签:c++ 设计模式
原文地址:http://blog.csdn.net/wwwdongzi/article/details/26610583