#include <iostream> using namespace std; class A { public: A(int a = 5) { cout<<"A constructor "<<this<<endl; } ~A() { cout<<"A destructor "<<this<<endl; } A(const A & r) { cout<<this<<" copy from "<<&r<<endl; } private: int a; }; A fun(A param) { A ret = param; return ret; } int main(int argc,char**argv) { cout<<"\nfun(1),cout<<\"out\":\n"; fun(1),cout<<"out"<<endl; cout<<"\n\nA a = fun(1):\n"; A a = fun(1); return 0; }
以g++ XX.cpp -fno-elide-constructors 运行,解除优化。
C++中 简单查看临时对象,局部对象的生命周期,及拷贝构造函数(测试代码)
时间: 2024-12-29 11:22:58