http://blog.csdn.net/silyvin/article/details/8985323
对应与CArray,补充一个vector的测试。
class A { int *m_a; int bb; private: A(); public: A(int n) { m_a = new int; *m_a = n; } A(A const & _a) { m_a = 0; *this = _a; } ~A() { delete m_a; } A &operator=(A const &_a) { if(this == &_a) return *this; if(m_a) delete m_a; m_a = new int; *m_a = *_a.m_a; return *this; } };
vector<A> arr; A a(1); arr.push_back(a); int fenge = 10; arr.erase(arr.begin()); fenge = 9;
push_back时生成对象:
0x0012fb48
0x003ba320
两次调用 copy构造,
然后删除第一个临时对象0x0012fb48
erase时调用第二个对象,也就是在容器中对象的析构函数。
如果将vector换成list,则push_back时仅调用一次copy构造。