C++临时对象
2014-10-05 09:40
211 查看
#include <iostream> using namespace std; class Test { int m_i; int m_j; static int count; public: Test() { count++; cout<<"调用无参构造函数"<<endl; cout<<"Test()"<<endl; } Test(int v) { count++; cout<<"调用int参构造函数"<<endl; cout<<"Test(int v)"<<endl; } ~Test() { cout<<"~Test()"<<endl; } static void output() { cout<<count<<endl; } }; int Test::count = 0; Test Play(Test t) { return t; } int main() { //Test a = Play(5); //在使用一个临时对象( 可能是无名对象 或者 返回对象值时 ) 创建构造另一个对象的过程的中,c++会优化掉该临时对象的产生, //直接以相同参数调用相关构造函数构或者 直接调用拷贝构造函数 到 目标对象. Test a; Test b = Play(a);//这里优化掉临时对象的产生?? //Test a(Play(5)); // Play(5); //Play(5).output(); }
还是不太理解这道题,难道是因为编译器优化的问题?
先记下来。
相关文章推荐
- C++临时对象的生命周期
- [More Effective C++]条款十九:理解临时对象的来源
- 【翠字营原创】: C++ 临时对象分析小结 涉及函数参数值传递、函数返回值、仿函数
- C++中的临时对象
- C++中的临时对象 zz
- 关于C++中的临时对象问题
- C++ 中临时对象的陷阱
- C++中的临时对象
- 【转】c++临时对象与const
- C++中的临时对象,对临时对象的引用,和临时对象的指针
- C++中的临时对象
- C++中的临时对象
- c++中的临时对象
- C++ 临时对象
- 笔记--c++临时对象与const
- c++临时对象
- 浅析C++临时对象的产生相关问题
- C++中的临时对象(http://blog.chinaunix.net/u/11680/showart_316503.html)
- [More Effective C++]条款十九:理解临时对象的来源
- C++临时对象(1)