prototype原型(待完善)
模式:prototype 解决向量的深浅克隆
#pragma once
#ifndef _PROTOTYPE_H_
#define _PROTOTYPE_H_
class Prototype{
public:
virtual ~Prototype();
virtual Prototype* Clone() const = 0;
virtual void showData() = 0;
virtual void addOne() = 0;
protected:
Prototype();
public:
int *p;
};
class ConcretePrototype :public Prototype{
public:
ConcretePrototype();
ConcretePrototype(const ConcretePrototype& cp);
~ConcretePrototype();
Prototype* Clone() const;
void showData();
void addOne();
};
#endif //~_PROTOTYPE_H_
#include "prototype.h"
//Prototype.cpp
#include "Prototype.h"
#include <iostream>
using namespace std;
Prototype::Prototype(){
}
Prototype::~Prototype(){
}
Prototype* Prototype::Clone() const{
return 0;
}
ConcretePrototype::ConcretePrototype(){
this->p = new int[5];
for (int i = 0; i < 5; i++)
p[i] = i;
}
ConcretePrototype::~ConcretePrototype(){
delete[] p;
}
ConcretePrototype::ConcretePrototype(const ConcretePrototype& cp){
cout << "ConcretePrototype copy ..." << endl;
//浅赋值
this->p = cp.p;
//深赋值
/*this->p = new int[5];
int i;
for(i = 0; i < 5;i++)
this->p[i] = cp.p[i];*/
}
Prototype* ConcretePrototype::Clone() const{
return new ConcretePrototype(*this);
}
void ConcretePrototype::showData()
{
int i;
cout << "<";
for (i = 0; i < 2; i++)
cout <<p[i] << ",";
cout << ">";
}
void ConcretePrototype::addOne()
{
int i;
for (i = 0; i < 2; i++)
p[i] += 10;
}
#include "Prototype.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
Prototype* p = new ConcretePrototype();
Prototype* p1 = p->Clone();
cout << "Before:" << endl;
cout << "p:";
p->showData();
cout << endl;
cout << "p1:";
p1->showData();
cout << endl;
p1->addOne();
cout << "After:" << endl;
cout << "p:";
p->showData();
cout << endl;
cout << "p1:";
p1->showData();
cout << endl;
system("pause");
cin.get();
return 0;
}
最新文章
- [No0000A0]批处理命令学习之:常用的特殊符号
- jdbcTemplate 泛型 查询
- 2437: [Noi2011]兔兔与蛋蛋 - BZOJ
- mysql5.7的安装
- Codevs 1648 最大和
- Python的面向对象4
- NewRowNeeded和UserAddedRow事件以及RowsAdded的区别使用
- [转]IDENT_CURRENT、SCOPE_IDENTITY、@@IDENTITY 差異對照表
- 数学之路-python计算实战(19)-机器视觉-卷积滤波
- 【夸QT十一】外来物品:通用脚本帮助Web运行基础Linux命令
- i春秋与我
- 创建一个可用的简单的SpringMVC项目,图文并茂
- Saltstack_使用指南06_远程执行-指定目标
- tex中pdf外链
- C#中关键字 &#39;User&#39; 附近有语法错误
- windows 7 安装时提示:安装程序无法创建新的系统分区
- C6.cpp
- 第二节,surf特征检测关键点,实现图片拼接
- 百度AI搜索引擎
- blfs(systemd版本)学习笔记-wget的安装与配置