C++ 复制构造函数和析构函数的无关调用
[后续行动]C++ 复制构造函数和析构函数的无关调用,c++,destructor,copy-constructor,C++,Destructor,Copy Constructor,[后续行动] 为了清楚地看到发生了什么,我建议在输出中包含this指针,以确定哪个A正在调用该方法 A() {cout<<"A (" << this << ") Construction" <<endl;} A(A const& a){cout<<"A (" << &a << "->" << this << ") Copy
为了清楚地看到发生了什么,我建议在输出中包含
this
指针,以确定哪个A正在调用该方法
A() {cout<<"A (" << this << ") Construction" <<endl;}
A(A const& a){cout<<"A (" << &a << "->" << this << ") Copy Construction"<<endl;}
~A() {cout<<"A (" << this << ") Destruction" <<endl;}
因此,流量可以解释为:
vector<A> t;
t.reserve(2); // <-- reserve space for 2 items.
t.push_back(A());
t.push_back(A());
A() {cout<<"A (" << this << ") Construction" <<endl;}
A(A const& a){cout<<"A (" << &a << "->" << this << ") Copy Construction"<<endl;}
~A() {cout<<"A (" << this << ") Destruction" <<endl;}
A (0xbffff8cf) Construction
A (0xbffff8cf->0x100160) Copy Construction
A (0xbffff8cf) Destruction
A (0xbffff8ce) Construction
A (0x100160->0x100170) Copy Construction
A (0xbffff8ce->0x100171) Copy Construction
A (0x100160) Destruction
A (0xbffff8ce) Destruction
A (0x100170) Destruction
A (0x100171) Destruction
vector<A> t;
t.reserve(2); // <-- reserve space for 2 items.
t.push_back(A());
t.push_back(A());
A (0xbffff8cf) Construction
A (0xbffff8cf->0x100160) Copy Construction
A (0xbffff8cf) Destruction
A (0xbffff8ce) Construction
A (0xbffff8ce->0x100161) Copy Construction
A (0xbffff8ce) Destruction
A (0x100160) Destruction
A (0x100161) Destruction