我正在尝试使用自定义容器,并在该容器的构造函数中传递了一个内存池分配器。整个事情是这样开始的:
AllocatorFactory alloc_fac;
//Creates a CPool allocator instance with the size of the Object class
IAllocator* object_alloc = alloc_fac.GetAllocator<CPool>(1000,sizeof(Object));
//Creates a CPool allocator instance with the size of the BList<Object> class
IAllocator* list_alloc = alloc_fac.GetAllocator<CPool>(10,sizeof(BList<Object>));
//Same logic in here as well
IAllocator* node_alloc = alloc_fac.GetAllocator<CPool>(1000,sizeof(BListNode<Object>));
IAllocator 类如下所示:
class IAllocator
{
public:
virtual void* allocate( size_t bytes ) = 0;
virtual void deallocate( void* ptr ) = 0;
template <typename T>
T* make_new()
{ return new ( allocate( sizeof(T) ) ) T (); }
template <typename T, typename Arg0>
T* make_new( Arg0& arg0 )
{ return new ( allocate( sizeof(T) ) ) T ( arg0 ); }
.......
}
容器类的构造函数如下所示:
template <class T>
class BList {
......
public:
/**
*@brief Constructor
*/
BList(Allocators::IAllocator& alloc){
_alloc = alloc;
reset();
}
/**
*@brief Constructor
*@param inOther the original list
*/
BList(const BList<T>& inOther){
reset();
append(inOther);
}
.....
}
当我这样做时:
BList<Object> *list = list_alloc->make_new<BList<Object>>(node_alloc);
编译器对此抱怨:
错误 1 错误 C2664: 'Containers::BList::BList(Allocators::IAllocator &)' : 无法将参数 1 从 'Allocators::IAllocator *' 转换为 'Allocators::IAllocator &' c:\licenta\licenta-transfer_ro -02may-430722\licenta\framework\framework\iallocator.h 21 框架
我想我用这个过头了....