I have a GameObjectFactory which creates/destroys a GameObject with the following methods:
mMemoryMgr is a custom memory allocation/deallocation module
template <class T>
inline static void DestroyObject(T* obj)
{
mMemoryMgr->Deallocate(obj);
}
// T is supposed to be a derived class of GameObject
template <class T>
inline boost::shared_ptr<T> CreateObject()
{
boost::shared_ptr<T> sPtr((T*) mMemoryMgr->Allocate(sizeof(T)),DestroyObject<T> );
// get a weak_ptr from shared_ptr and cast its object to gameobject
boost::weak_ptr<GameObject> wPtr = boost::static_pointer_cast<boost::shared_ptr<GameObject>>(sPtr);
// add it to some container of weak_ptr<gameobject>
mObject.push_back(wPtr);
return sPtr;
}
I have the following problems though with my design;
1. I get the following errors when trying to cast the shared_ptr<T> to weak_ptr<GameObject>:
Error 2 error C2440: 'initializing' : cannot convert from 'boost::shared_ptr<T>' to 'boost::weak_ptr<T>'
Why is this?
2. I'm new to template programming; Is there any way to 'enforce' in compile time in CreateObject() that T is derived from GameObject through a predicate or something?
such as template <class T where T is derived from GameObject>...
Thanks