1,assign会多次调用析构?
#include <stdlib.h>
#include <iostream>
using namespace std;
#include <boost/assign.hpp>
class tcbn
{
private:
int id;
public:
tcbn() { id = 0, cout << "init id= " << id << endl; }
tcbn(int x1) { id = x1, cout << "init id= " << id << endl; }
~tcbn() { cout << "des-init id= " << id << endl; }
int get_id() { return id; }
};
//tcbn make_tcbn(int x1) {return tcbn}
int main()
{
using namespace boost::assign;
//将3,1,4,1,5,9推入vint
vector<int> vint;
vint += 3, 1, 4, 1, 5, 9;
int vi = 0;
for (vi = 0; vi < vint.size(); vi++) cout << vint[vi] << endl;
//将若干个tcbn类推入vtcbn
{
vector <tcbn> vtcbn;
tcbn t1(17001);
tcbn t2(17002);
vtcbn += t1, t2;
for (vi = 0; vi < vtcbn.size(); vi++)
{
cout << vtcbn[vi].get_id() << endl;
}
}
cin >> vi;
}
上述代码的输出为
3
1
4
1
5
9
init id= 17001
init id= 17002
des-init id= 17001
des-init id= 17001
des-init id= 17001
des-init id= 17002
17001
17002
des-init id= 17002
des-init id= 17001
des-init id= 17001
des-init id= 17002
这太奇怪了吧!!!