Laz*_*zer 2 c++ destructor copy-constructor
[跟进这个问题 ]
class A
{
public:
A() {cout<<"A Construction" <<endl;}
A(A const& a){cout<<"A Copy Construction"<<endl;}
~A() {cout<<"A Destruction" <<endl;}
};
int main() {
{
vector<A> t;
t.push_back(A());
t.push_back(A()); // once more
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
A Construction // 1
A Copy Construction // 1
A Destruction // 1
A Construction // 2
A Copy Construction // 2
A Copy Construction // WHY THIS?
A Destruction // 2
A Destruction // deleting element from t
A Destruction // deleting element from t
A Destruction // WHY THIS?
Run Code Online (Sandbox Code Playgroud)
ken*_*ytm 16
为了清楚地看到发生了什么,我建议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;}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是
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
Run Code Online (Sandbox Code Playgroud)
所以流程可以解释为:
如果你这样做,第5步将会消失
vector<A> t;
t.reserve(2); // <-- reserve space for 2 items.
t.push_back(A());
t.push_back(A());
Run Code Online (Sandbox Code Playgroud)
输出将变为:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
352 次 |
| 最近记录: |