拷贝构造函数
来源:互联网 发布:网络语cx是什么意思 编辑:程序博客网 时间:2024/05/01 11:33
拷贝构造函数格式为
classname (const classname &object)
{
//do something
}
下面的程序可以说明问题,为什么拷贝构造函数是必要的
class samp
{
char * s;
int num;
public:
samp(int i)
{
s='/0';
num = i;
cout << num << endl;
}
//拷贝构造函数
samp(samp& obl)
{
int l = strlen(obl.s);
s = new char[l + 1];
strcpy(s, obl.s);
}
samp()
{
}
~samp()
{
if (s)
delete[] s;
cout << "Freeing s[" << num << "]" << endl;
}
void show()
{
cout << s << "/n";
}
void set(char * str);
};
void samp::set(char * str)
{
s = new char[strlen(str) + 1];
if (!s)
{
cout << " allocation error./n";
exit(1);
}
strcpy(s, str);
}
void main(void)
{
samp ob;
ob = input();
ob.show();
return 0;
}
- 构造函数、拷贝构造函数
- 拷贝构造函数,构造函数
- 拷贝构造函数--默认拷贝构造函数
- 构造、拷贝构造函数基础
- 拷贝构造函数 深拷贝 浅拷贝
- 拷贝构造函数(深拷贝,浅拷贝)
- 拷贝构造函数浅拷贝深拷贝
- 拷贝构造函数,浅拷贝,深拷贝
- 拷贝构造函数 深拷贝 浅拷贝
- 拷贝构造函数----深拷贝、浅拷贝
- 拷贝构造函数
- 拷贝构造函数
- 浅析拷贝构造函数
- 拷贝构造函数
- 拷贝构造函数
- C++拷贝构造函数
- C++拷贝构造函数
- 拷贝构造函数
- c&z studio成立,关于c&z
- 中国式报表,头大!
- 输出文件的下载
- Java加密和数字签名编程快速入门
- 试验 vs 实验
- 拷贝构造函数
- [ZT]理光R1V夜景拍摄技巧
- [ZT]如何拍摄好玻璃缸内的游鱼
- vlc学习计划(6)--网络数据流接收处理过程分析
- Oracle数据库中日期的计算方法
- [笔记]《Computer Architecture: A Quantitative Approach (2nd)》ch1-2
- Form开发中的图片存储
- oracle中字符集的相关问题
- Microsoft .NET Compact Framework Windows Mobile个人所得税计算小应用【附源码】