备忘之 拷贝构造函数
来源:互联网 发布:淘宝网店怎么设置客服 编辑:程序博客网 时间:2024/06/01 16:14
//拷贝构造函数,什么时候算是 “合适的时候”,才会合成呢~~~
#include <iostream>
using namespace std;
class A
{
public:
A(char *_str):str(_str),x(0){}
//A(const A&a){cout << "call default copy constructor..." << endl;} //如果定义了的话,那么编译器不会合成。
//如果没有定义的话,对于类A来说,编译器依然不会合成,因为此时的对象之间的复制很简单,就是一个int与一个char *的复制。
//此时,你可能已经注意到了,这个类含有指针,应该自己定义复制控制函数。但这并不是我本篇文章想要强调的东西。
~A(){}
int getX() const {return x;}
char *getStr() const {return str;}
private:
int x;
char *str;
};
int main()
{
A a("123");
A b = a;
cout << b.getX() << " " << a.getX() << endl;
cout << b.getStr() << " " << b.getStr() << endl;
return 0;
#include <iostream>
using namespace std;
class A
{
public:
A(char *_str):str(_str),x(0){}
//A(const A&a){cout << "call default copy constructor..." << endl;} //如果定义了的话,那么编译器不会合成。
//如果没有定义的话,对于类A来说,编译器依然不会合成,因为此时的对象之间的复制很简单,就是一个int与一个char *的复制。
//此时,你可能已经注意到了,这个类含有指针,应该自己定义复制控制函数。但这并不是我本篇文章想要强调的东西。
~A(){}
int getX() const {return x;}
char *getStr() const {return str;}
private:
int x;
char *str;
};
int main()
{
A a("123");
A b = a;
cout << b.getX() << " " << a.getX() << endl;
cout << b.getStr() << " " << b.getStr() << endl;
return 0;
}
2 0
- 备忘之 拷贝构造函数
- 基础备忘:拷贝构造函数和赋值函数
- C++之拷贝构造函数
- 拷贝构造函数之我见
- C++之拷贝构造函数
- C++之拷贝构造函数
- C++之拷贝构造函数
- c++之拷贝构造函数
- C++之拷贝构造函数
- C++之拷贝构造函数
- C++之拷贝构造函数
- C++之“拷贝构造函数”
- c++构造函数之拷贝构造
- C++拷贝构造函数之深拷贝、浅拷贝
- C++学习之拷贝构造函数(深拷贝,浅拷贝)
- 拷贝构造函数之深拷贝浅拷贝
- 基础备忘:关于构造函数、拷贝构造函数、析构函数
- C++之拷贝构造函数1
- MySQL源码解读
- 用matlab 对序列卷积算法的验证
- 让创意更有黏性!
- uva 11538 - Chess Queen(数论)
- SELECT超时小结
- 备忘之 拷贝构造函数
- [hdu4041]Eliminate Witches!
- struts2使用JSP直接访问国际化资源文件
- poj_A Chess Game2425
- uva 103Stacking Boxes
- PAT 1006. 换个格式输出整数
- 《程序员面试宝典》打印规律矩阵
- 单例模式的七种写法
- Socket通信(TCP)非阻塞模式-select模型