#include<iostream>
#include <vector>
using namespace std;
class A {
public:
A (int i = 0) : m_i (i) {
cout << "无参构造:" << this << endl;
}
A (const A& that) : m_i (that.m_i) {
cout << "拷贝构造:" << &that << "->" << this
<< endl;
}
A& operator= (const A& that) {
cout << "拷贝赋值:" << &that << "->" << this
<< endl;
if (&that != this)
m_i = that.m_i;
return *this;
}
~A (void) {
cout << "析构函数:" << this << endl;
}
private:
int m_i;
};
int main (void) {
vector<A> va (3);
return 0;
}
从上边的运行结果可以看出vector是先用无参构造在栈里面构造了一个临时变量,再用临时变量做拷贝源,在堆里面用拷贝构造的方式创建了三个对象,所以才有四个构造加四个析构。