【发布时间】:2012-03-31 17:36:49
【问题描述】:
我有一个关于向boost::shared_ptr 构造函数提供自定义删除方法的问题。
例如,我有一个 GameObjectFactory 类,它创建/销毁 GameObjects。它有一个MemoryManager的实例,可以Allocate()/Deallocate()内存。 CreateObject()返回一个GameObject,通过MemoryManager分配,封装在一个boost::shared_ptr中。
当boost::shared_ptr 破坏时,它应该调用我的MemoryManager->Deallocate() 方法。但是我做错了;我收到这些错误:
error C2276: '&' : illegal operation on bound member function expression
error C2661: 'boost::shared_ptr<T>::shared_ptr' : no overloaded function takes 2 arguments
我已经阅读了 boost 文档以及我从 stackoverflow 获得的点击量,但我无法正确理解。我不明白为什么以下不工作。
这是我的代码;
#ifndef _I_GAMEOBJECT_MANAGER_H
#define _I_GAMEOBJECT_MANAGER_H
#include "../../Thirdparty/boost_1_49_0/boost/smart_ptr/shared_ptr.hpp"
#include "EngineDefs.h"
#include "IMemoryManager.h"
#include "../Include/Core/GameObject/GameObject.h"
namespace Engine
{
class IGameObjectFactory
{
public:
virtual ~IGameObjectFactory() { }
virtual int32_t Init() = 0;
virtual bool Destroy() = 0;
virtual bool Start() = 0;
virtual bool Stop() = 0;
virtual bool isRunning() = 0;
virtual void Tick() = 0;
template <class T>
inline boost::shared_ptr<T> CreateObject()
{
boost::shared_ptr<T> ptr((T*) mMemoryMgr->Allocate(sizeof(T)),&mMemoryMgr->Deallocate);
return ptr;
}
template <class T>
inline boost::shared_ptr<T> CreateObject(bool UseMemoryPool)
{
boost::shared_ptr<T> ptr((T*) mMemoryMgr->Allocate(sizeof(T),UseMemoryPool), &mMemoryMgr->Deallocate);
return ptr;
}
protected:
IMemoryManager* mMemoryMgr;
};
}
#endif
【问题讨论】:
标签: c++ memory-management boost shared-ptr smart-pointers