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
这太奇怪了吧!!!
本文详细解析了一段使用C++和boost库的代码片段,重点讨论了析构函数的调用过程以及boost::assign库在向容器中插入元素时的行为。通过实例演示了如何在代码中正确地利用这些特性。

被折叠的 条评论
为什么被折叠?



