使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; }
不过看起来也挺别扭的,希望不要写带太多参数的构造函数,如果实在多,数据之间又关联,可以定义成结构体,避免增加参数个数。
相关文章推荐
- php 构造函数支持不同个数参数的方法
- php 构造函数支持不同个数参数的方法
- Unity实现支持泛型的事件管理以减少使用object作为参数带来的频繁装拆箱
- 4.构造方法与重载:定义一个网络用户类,要处理的信息有用户 ID、用户密码、 email 地址。在建立类的实例时把以上三个信息都作为构造函数的参数输入, 其中用户 ID 和用户密码时必须缺省时 ema
- 仅IE不支持setTimeout/setInterval函数的第三个以上参数
- html自定义垂直导航菜单(加强版--自定义传入menu参数,支持JSONArray、JSArray、JSONObject、JSObject)
- 仅IE不支持setTimeout/setInterval函数的第三个以上参数
- Android Studio 2.2以上支持了Cmake的配置JNI的相关参数
- ActiveXObject 所支持的参数对象部分列表(自带)
- php 构造函数支持不同个数参数的方法
- Aop动态生成代理类时支持带参数构造函数
- object_c 如何扩写NSObject 中的performSelector 方法实现传三个以上的参数
- Extjs DateTime 日期时间选择控件 (非点击日期强制选择) 支持4.0以上
- 轻松接触MySQL数据库支持的3个引擎
- [C++基础]在子类中向父类的构造函数传递参数的小例子,包括类中常量的初始化
- 第五周上机任务项目1-(1带参数构造函数)--(纪念原创博文第100篇)
- 支持IE6以上阴影效果纯CSS
- 一个分页存储过程,Sql2005及以上版本支持(求更高效的分页算法)
- 定义Point类的无参数构造函数,将point对象的数据成员初始化为0。