2
[跟進到this question]外部調用拷貝構造函數和析構函數
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
}
}
輸出是:
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?
@KennyTM:極好的方法和解釋!謝謝! – Lazer 2010-04-17 08:39:52