临时对象与拷贝构造函数
来源:互联网 发布:淘宝网店怎么进货 编辑:程序博客网 时间:2024/06/05 09:56
拷贝构造函数主要应用在以下场景中:
1 根据一个同类型的对象新建一个对象时。
注意与=赋值运算符的区别,=不会生成一个新的对象。下面实例:
运行结果:
default constructing...
constructing with parameter ...1
operatoring...
copy constructing...
2 复制一个对象,将它作为实参传给一个函数(注意对象引用不会)。
3 从函数返回时复制一个对象(注意对象引用不会)。下面实例:
运行结果:
default constructing...
play1
copy constructing...//实参传给一个函数
copy constructing...//返回一个对象
play2
copy constructing...//返回一个对象
play3
copy constructing...//实参传给一个函数
play4
下面是一道面试题:
运行结果:
constructing with parameter ...1//使用1新建实参s
copy constructing...//返回一个对象,调用复制构造函数
destructing with parameter ...1//参数s析构
destructing with parameter ...1//调用函数main没有定义变量接收返回值,所以会用一个临时对象保存返回的对象的值。被调用函数结束时,临时对象被析构
......
constructing with parameter ...2//使用2新建实参s
copy constructing...//返回一个对象,调用复制构造函数
destructing with parameter ...2//参数s析构
......
default constructing...//Single o3;
constructing with parameter ...3//使用3新建实参s
copy constructing...//返回一个对象,调用复制构造函数
destructing with parameter ...3//参数s析构
operatoring...//赋值
destructing with parameter ...3//临时对象析构
destructing with parameter ...3//o3析构
destructing with parameter ...2//o2析构
下面的例子来自Thinking in C++:
#include <cstdio>#include <iostream>#include <fstream>using namespace std;ofstream out("howmany2.out");class HowMany2 {string name; // Object identifierstatic int objectCount;public:HowMany2(const string& id = "") : name(id) {++objectCount;print("HowMany2()");}~HowMany2() {--objectCount;print("~HowMany2()");}// The copy-constructor:HowMany2(const HowMany2& h) : name(h.name) {name += " copy";++objectCount;print("HowMany2(const HowMany2&)");}void print(const string& msg = "") const {if(msg.size() != 0)out << msg << endl;out << '\t' << name << ": "<< "objectCount = "<< objectCount << endl;}};int HowMany2::objectCount = 0;// Pass and return BY VALUE:HowMany2 f(HowMany2 x) {x.print("x argument inside f()");out << "Returning from f()" << endl;return x;}int main() {HowMany2 h("h");out << "Entering f()" << endl;HowMany2 h2 = f(h);h2.print("h2 after call to f()");out << "Call f(), no return value" << endl;f(h);out << "After call to f()" << endl;} ///:~
运行的结果为:
HowMany2()
h: objectCount = 1
Entering f()
HowMany2(const HowMany2&)
h copy: objectCount = 2
x argument inside f()
h copy: objectCount = 2
Returning from f()
HowMany2(const HowMany2&)
h copy copy: objectCount = 3
~HowMany2()
h copy: objectCount = 2
h2 after call to f()
h copy copy: objectCount = 2
Call f(), no return value
HowMany2(const HowMany2&)
h copy: objectCount = 3
x argument inside f()
h copy: objectCount = 3
Returning from f()
HowMany2(const HowMany2&)
h copy copy: objectCount = 4
~HowMany2() //临时对象
h copy copy: objectCount = 3
~HowMany2() //内部对象
h copy: objectCount = 2
After call to f()
~HowMany2()
h copy copy: objectCount = 1
~HowMany2()
h: objectCount = 0
其中临时对象的析构要早于内部对象,而Thinking in C++中顺序相反。总之,临时对象的生存期一定要尽可能的短。
- 临时对象与拷贝构造函数
- 临时对象与拷贝构造函数
- 【题目】C++拷贝构造函数与C++临时对象
- 返回临时对象时的拷贝构造函数问题
- C++中的临时对象(拷贝构造函数)(上)
- C++中的临时对象(拷贝构造函数)(下)
- C++中的临时对象(拷贝构造函数)(上)
- C++中的临时对象(拷贝构造函数)(下)
- C++中的临时对象(拷贝构造函数)
- object构造、拷贝构造、析构、临时对象
- 临时对象的(构造函数与析构函数)
- 关于C++中在函数中按值返回、临时对象、拷贝构造函数的一点理解
- C++拷贝构造函数、无名临时对象以及NRV优化分析
- 函数返回类的对象与拷贝构造函数
- C++类对象之间复制,拷贝构造函数的作用,深拷贝与浅拷贝
- 拷贝构造函数 与 拷贝赋值函数
- 拷贝构造函数 与拷贝复制函数
- 拷贝构造函数与赋值构造函数
- 淘宝iDataForum归来
- 11.05.29 ~ 11.06.04
- Java连接各种数据库方法
- 排序算法
- 有关ajax的一个小错误
- 临时对象与拷贝构造函数
- 未能加载文件或程序集
- Rokon游戏引擎停止更新
- Windows端口大全
- 关于const常量的地址经过const_cast转换之后,发生了什么
- arm-linux-gcc 安装
- sizeof的用法
- libc.so.6 缺失问题
- 使用嵌入式linux完全手册光盘的arm-linux-gcc 遇到问题 自己编译