c++深拷贝+装饰者模式+内存释放
时间:2011-12-06
来源:互联网
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
string m_name;//为了调试,访问属性破坏了封装性
Person(){ //构造函数
cout<<this<<" normal constructor of person class "<<endl;
}
Person(string name){//重载构造函数
cout<<this<<" normal constructor of person class "<<endl;
m_name = name;
}
virtual ~Person(){
cout<<this <<" Person destroy---"<<endl;
}
virtual void showName(){
cout<<" Decorator "<<m_name<<endl;
}
};
class Finery : public Person{
public:
//为了调试,访问属性破坏了封装性
Person *m_component;
Finery(){ //构造
cout<<" normal constructor of finery "<<endl;
}
virtual ~Finery(){ //析构函数
cout<<&m_component<<" Finery destroy---"<<endl;
delete m_component;
}
void Decorate(Person *component){ //装饰
m_component = component; //浅拷贝
}
virtual void showName(){ //成员函数
m_component->showName();
}
};
class Tshirts : public Finery{
public:
Tshirts(){
cout<<"constructor of Tshirts "<<endl;
}
virtual ~Tshirts(){
cout<<" Tshirts destroy "<<endl;
}
virtual void showName(){
cout<<"Tshirt ";
Finery::showName();
}
};
class BigTrouser:public Finery{
public:
BigTrouser(){
cout<<"constructor of BigTrouser "<<endl;
}
virtual ~BigTrouser(){
cout<<" bigtrouser destroy "<<endl;
}
virtual void showName(){
cout<<"BigTrouser ";
Finery::showName();
}
};
//客户端
int main()
{
Person *p=new Person("小李");
BigTrouser *bt=new BigTrouser();
Tshirts *ts=new Tshirts();
bt->Decorate(p);
ts->Decorate(bt);
ts->showName();
delete ts;
/* 我想像下面这样写,可是上面的delete已经释放了他们共同指向的Person,
怎样写拷贝构造函数才能用下面的 delete来释放内存呢?
delete bt;
delete p; */
return 0;
}
#include<string>
using namespace std;
class Person{
public:
string m_name;//为了调试,访问属性破坏了封装性
Person(){ //构造函数
cout<<this<<" normal constructor of person class "<<endl;
}
Person(string name){//重载构造函数
cout<<this<<" normal constructor of person class "<<endl;
m_name = name;
}
virtual ~Person(){
cout<<this <<" Person destroy---"<<endl;
}
virtual void showName(){
cout<<" Decorator "<<m_name<<endl;
}
};
class Finery : public Person{
public:
//为了调试,访问属性破坏了封装性
Person *m_component;
Finery(){ //构造
cout<<" normal constructor of finery "<<endl;
}
virtual ~Finery(){ //析构函数
cout<<&m_component<<" Finery destroy---"<<endl;
delete m_component;
}
void Decorate(Person *component){ //装饰
m_component = component; //浅拷贝
}
virtual void showName(){ //成员函数
m_component->showName();
}
};
class Tshirts : public Finery{
public:
Tshirts(){
cout<<"constructor of Tshirts "<<endl;
}
virtual ~Tshirts(){
cout<<" Tshirts destroy "<<endl;
}
virtual void showName(){
cout<<"Tshirt ";
Finery::showName();
}
};
class BigTrouser:public Finery{
public:
BigTrouser(){
cout<<"constructor of BigTrouser "<<endl;
}
virtual ~BigTrouser(){
cout<<" bigtrouser destroy "<<endl;
}
virtual void showName(){
cout<<"BigTrouser ";
Finery::showName();
}
};
//客户端
int main()
{
Person *p=new Person("小李");
BigTrouser *bt=new BigTrouser();
Tshirts *ts=new Tshirts();
bt->Decorate(p);
ts->Decorate(bt);
ts->showName();
delete ts;
/* 我想像下面这样写,可是上面的delete已经释放了他们共同指向的Person,
怎样写拷贝构造函数才能用下面的 delete来释放内存呢?
delete bt;
delete p; */
return 0;
}
作者: xiaoyanghuaban 发布时间: 2011-12-06
看得有点乱
m_component = component; //浅拷贝
既然你知道这里是浅拷贝,那你怎么不new一个新空间呢?
m_component = component; //浅拷贝
既然你知道这里是浅拷贝,那你怎么不new一个新空间呢?
作者: tujiaw 发布时间: 2011-12-06
撇开其他不说,如果要深拷贝的话:
void Decorate(Person *component){
//先释放原来空间
if (m_component) delete m_component;
//重新分配空间并初始化
m_component = new Person(*component);
}
以上只是一个基本原型,前提是Person要有拷贝构造函数Person(const Person&)。代码中还是有不少问题哦。
void Decorate(Person *component){
//先释放原来空间
if (m_component) delete m_component;
//重新分配空间并初始化
m_component = new Person(*component);
}
以上只是一个基本原型,前提是Person要有拷贝构造函数Person(const Person&)。代码中还是有不少问题哦。
作者: tonywearme 发布时间: 2011-12-06
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28