BOOST object_pool自定义类模板问题

debugii 2009-09-20 02:42:21
cboostpool.h

#include <windows.h>
#include <boost/pool/object_pool.hpp>
template <class T>
class BOOSTPOOL{
public:
boost::object_pool<T> _objpool;
void* operator new( size_t size )
{

return _objpool.construct();
}

void operator delete( void* p )
{
_objpool.destroy(p);
}

};

-------------------------------------------

User.h

#pragma once
#include "cboostpool.h"
class CUser:public BOOSTPOOL<CUser>
{
public:
CUser(void);
~CUser(void);
};

--------------------------------------------

ccc.cpp


#include "stdafx.h"
#include "User.h"

int _tmain(int argc, _TCHAR* argv[])
{
CUser *p=new CUser();
return 0;
}


编译总是出错:
1>ccc.cpp
1>e:\boosttest\ccc\ccc\cboostpool.h(10) : error C2228: “.construct”的左边必须有类/结构/联合
1> e:\boosttest\ccc\ccc\cboostpool.h(8): 编译类 模板 成员函数“void *BOOSTPOOL<T>::operator new(size_t)”时
1> with
1> [
1> T=CUser
1> ]
1> e:\boosttest\ccc\ccc\user.h(4): 参见对正在编译的类 模板 实例化“BOOSTPOOL<T>”的引用
1> with
1> [
1> T=CUser
1> ]
1>生成日志保存在“file://e:\boosttest\ccc\ccc\Debug\BuildLog.htm”
1>ccc - 1 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========



请高手指点
...全文
249 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yshuise 2009-09-22
  • 打赏
  • 举报
回复
如果一定要重载就不能调用consruct函数,而是malloc;
#include "stdafx.h"
#include <iostream>
#include <boost/pool/object_pool.hpp>
using namespace std;
template <class T>
class BOOSTPOOL{
public:
static boost::object_pool<T> _objpool;
static void * operator new(size_t size){
return _objpool.malloc();
}
static void operator delete(void *rawmemory, size_t size){
_objpool.free((T*)rawmemory);
}

};

class CUser:public BOOSTPOOL <CUser>
{
public:
using BOOSTPOOL<CUser>::operator new;
using BOOSTPOOL<CUser>::operator delete;
CUser(void){};
~CUser(void){};
void output(){cout<<"hello"<<endl;}
};

boost::object_pool<CUser> BOOSTPOOL<CUser>::_objpool;

int _tmain(int argc, _TCHAR* argv[])
{
CUser* cuser = new CUser;
cuser->output();
return 0;

}
cattycat 2009-09-21
  • 打赏
  • 举报
回复
楼上高手。
yshuise 2009-09-21
  • 打赏
  • 举报
回复
// ttt.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <boost/pool/object_pool.hpp>
using namespace std;
template <class T, class allocate = boost::object_pool<T>>
class BOOSTPOOL{
public:
allocate _objpool;
T* construct()
{

return _objpool.construct();
}

void destroy( void* p)
{
_objpool.destroy(p);
}

};

class CUser:public BOOSTPOOL <CUser>
{
public:
CUser(void){};
~CUser(void){};
void output(){cout<<"hello"<<endl;}
};



int _tmain(int argc, _TCHAR* argv[])
{
BOOSTPOOL<CUser> a;//a可以构造任何模板参数对象。
CUser* p = a.construct();
p->output();
return 0;

}


hello
请按任意键继续. . .

yshuise 2009-09-21
  • 打赏
  • 举报
回复
construct连分配内存和构造对象都有。根本不用重载了。
debugii 2009-09-21
  • 打赏
  • 举报
回复
但是还是没有重载 new 和 delete啊,我是像直接重载掉new 和 delete
yshuise 2009-09-20
  • 打赏
  • 举报
回复
现在没法调试,不方便。
debugii 2009-09-20
  • 打赏
  • 举报
回复
抽出单独的文件?我只想用类模板重载NEW 和 DELETE
yshuise 2009-09-20
  • 打赏
  • 举报
回复
为什么抽出单独的文件?
debugii 2009-09-20
  • 打赏
  • 举报
回复
#include <windows.h>
#include <boost/pool/object_pool.hpp>
template <class T>
class BOOSTPOOL{
public:
static boost::object_pool<T> _objpool; <--------将它定义成静态变量
void* operator new( size_t size )
{

return _objpool.construct();
}

void operator delete( void* p )
{
_objpool.destroy((T*)p); <--------强制转换
}


最终还是报错:
1>j:\streetdunkcode\workserver\libs\boost_1_39_0\boost\pool\object_pool.hpp(74) : error C2660: “BOOSTPOOL<T>::operator new”: 函数不接受 2 个参数
1> with
1> [
1> T=CUser
1> ]
1> j:\streetdunkcode\workserver\libs\boost_1_39_0\boost\pool\object_pool.hpp(70): 编译类 模板 成员函数“CUser *boost::object_pool<T>::construct(void)”时
1> with
1> [
1> T=CUser
1> ]
1> e:\boosttest\ccc\ccc\cboostpool.h(6): 参见对正在编译的类 模板 实例化“boost::object_pool<T>”的引用
1> with
1> [
1> T=CUser
1> ]
1> e:\boosttest\ccc\ccc\user.h(4): 参见对正在编译的类 模板 实例化“BOOSTPOOL<T>”的引用
1> with
1> [
1> T=CUser
1> ]
1>生成日志保存在“file://e:\boosttest\ccc\ccc\Debug\BuildLog.htm”
1>ccc - 1 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========

};


继续高手求助

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧