运算符之深度复制拷贝
来源:互联网 发布:梓潼淘宝运营招聘 编辑:程序博客网 时间:2024/05/22 01:27
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class bb
{
public:
char *i;
bb ()
{this->i=0;
printf("no pram!\n");
}
bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
printf("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
printf("destry!\n");
}
};
int main()
{
bb a;
a="tom";
return 0;
}
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class bb
{
public:
char *i;
bb ()
{this->i=0;
printf("no pram!\n");
}
bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
printf("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
printf("destry!\n");
}
};
int main()
{
bb a("tom");
bb a1=a;//属于浅度复制
return 0;
}
以上这里就会出错,2次运行析构函数,对同一个地方2次销毁,所以就会出错,则用赋值运算符重载来进行复制。
加上这个就不会出错啦
bb & operator=(bb &b)
{
this->i=new char[10];
strcpy(this->i,b.i);
return *this;
}
对于这样,要拷贝构造函数
bb (bb &b)//一定要&,不然会进入死循环
{this->i=new char[10];
strcpy(this->i,b.i);
printf("深度复制!\n");
}
总的为
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class bb
{
public:
char *i;
bb ()
{this->i=0;
printf("no pram!\n");
}
bb (bb &b)//
{this->i=new char[10];
strcpy(this->i,b.i);
printf("深度复制!\n");
}
bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
printf("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
printf("destry!\n");
}
bb & operator=(bb &b)
{
this->i=new char[10];
strcpy(this->i,b.i);
return *this;
}
};
int main()
{
bb a("tom"),a1;
a1=a;
bb a2=a1;
return 0;
}
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class bb
{
public:
char *i;
bb ()
{this->i=0;
printf("no pram!\n");
}
bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
printf("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
printf("destry!\n");
}
};
int main()
{
bb a;
a="tom";
return 0;
}
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class bb
{
public:
char *i;
bb ()
{this->i=0;
printf("no pram!\n");
}
bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
printf("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
printf("destry!\n");
}
};
int main()
{
bb a("tom");
bb a1=a;//属于浅度复制
return 0;
}
以上这里就会出错,2次运行析构函数,对同一个地方2次销毁,所以就会出错,则用赋值运算符重载来进行复制。
加上这个就不会出错啦
bb & operator=(bb &b)
{
this->i=new char[10];
strcpy(this->i,b.i);
return *this;
}
对于这样,要拷贝构造函数
bb (bb &b)//一定要&,不然会进入死循环
{this->i=new char[10];
strcpy(this->i,b.i);
printf("深度复制!\n");
}
总的为
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class bb
{
public:
char *i;
bb ()
{this->i=0;
printf("no pram!\n");
}
bb (bb &b)//
{this->i=new char[10];
strcpy(this->i,b.i);
printf("深度复制!\n");
}
bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
printf("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
printf("destry!\n");
}
bb & operator=(bb &b)
{
this->i=new char[10];
strcpy(this->i,b.i);
return *this;
}
};
int main()
{
bb a("tom"),a1;
a1=a;
bb a2=a1;
return 0;
}
0 0
- 运算符之深度复制拷贝
- C++之拷贝构造函数和复制运算符重载
- 《深度探索c++ 对象模型》有感之默认构造函数,拷贝构造函数,拷贝运算符的合成
- Item 12:完整地拷贝对象(拷贝构造函数、复制运算符) Effective C++笔记
- 赋值运算符复制构造函数(拷贝构造函数)易混处总
- 赋值运算符复制构造函数(拷贝构造函数)易混处总结
- C++中拷贝(复制)构造函数和赋值运算符重载函数
- 异常安全的复制构造函数和拷贝赋值运算符函数
- 【C++】复制构造函数(拷贝构造函数)和=运算符重载使用的区别
- python之深度、浅复制
- IE WebBrowser 深度复制拷贝/三种遍历IFRAME方式
- 函数传值 复制构造函数 深度拷贝
- C#:浅谈对象数组,运算符重载和深度复制的应用
- C#:浅谈对象数组,运算符重载和深度复制的应用
- C++之--拷贝(复制)构造函数
- 复制运算符
- 合成拷贝赋值运算符
- 【C++】拷贝构造函数之浅复制与深复制
- 如何在Ubuntu上安装及简单配置SVN
- Windows客户端连接不上Linux服务器
- LA 3026(Period-MP算法)[Template:KMP]
- 开源Math.NET基础数学类库使用(08)C#进行数值积分
- pinyin4j把中文句子(含有多音字字母)转成拼音(二维数组递归求所有组合情况返回list)算法实现!
- 运算符之深度复制拷贝
- 安装Android Studio问题汇总
- 黑马程序员—继承与多态
- poj3311 全排列
- 用户空间应用程序如何加载模块到内核空间?
- Oracle数据泵
- 8 SQLiteDatabase 存储时需要使用绝对路径
- Hbase与MapReduce
- 初试win8商店应用开发_三姑六婆亲属关系计算器