您的位置:首页 > 移动开发 > Objective-C

使object_pool支持3个以上构造函数参数

2013-01-15 10:57 239 查看
boost中的object_pool的construct函数最多支持3个参数,但是可以扩展,我用了辅助函数模板的方式:

#include <boost/pool/object_pool.hpp>
using namespace boost;

class demo_class
{
public:
int x,y,z,w;
demo_class(int x=1,int y=2,int z=3,int w=4):x(x),y(y),z(z),w(w) {}
void show() {cout<<x<<" "<<y<<" "<<z<<" "<<w<<endl;}
};

template <typename P,typename T0,typename T1,typename T2,typename T3>
inline typename P::element_type* construct(P& p,const T0 & a0, const T1 & a1, const T2 & a2,const T3& a3)
{
typename P::element_type* mem=p.malloc();
assert(mem!=NULL);
new (mem)P::element_type(a0,a1,a2,a3);
return mem;
}

int _tmain(int argc, _TCHAR* argv[])
{
object_pool<demo_class> opl;
demo_class* p=construct<object_pool<demo_class>,int,int,int,int>(opl,1,2,3,4);
p->show();
return 0;
}


不过看起来也挺别扭的,希望不要写带太多参数的构造函数,如果实在多,数据之间又关联,可以定义成结构体,避免增加参数个数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐