Question

i have interface that looks like this :

class IGameObject
{
public:
    virtual ~IGameObject(){}
    virtual void Notify(Massage message) = 0;
    virtual void SendMessages() = 0;
};

class WinFrameObj :public Sprite , public BaseGameObject<WinFrameObj>
{
    public:

        WinFrameObj();
        virtual ~WinFrameObj(){};
        static WinFrameObj* createInternal();        
        void Notify(Massage message);

};
 template<typename T>
class BaseGameObject : public IGameObject
{
    public:
        BaseGameObject(){};

        virtual ~BaseGameObject(){};

        static T* createObj()
        {
            return T::createInternal();
        }

};

// Simple catch class 
typedef std::map<GameObjectType, IGameObject*> ComponentsMap;
class ComponentMadiator{  

.... 
....
void ComponentMadiator::Register(const GameObjectType gameObjectType,IGameObject*& gameObj)
{
   componentsMap[GameObjectType] = gameObj;  // THIS is std map
}
...
...

}

now i do in code in the main class

WinFrameObj* m_pMainWindowFrameObjCenter ; // defined in the header as memeber 
pMainWindowFrameObjCenter  = WinFrameObj::createObj();
ComponentMadiator::Instance().Register(MAIN_WIN_FRAME,pMainWindowFrameObjCenter); 

im getting this error:

error C2664: 'ComponentMadiator::Register' : cannot convert parameter 2 from 'WinFrameObj *' to 'IGameObject *&'

i need the ComponentMadiator::Register method to be generic there are allot of objects that are from type IGameObject , it have to be by reference to a pointer

UPDATE
the reason i do this is to keep the data i store in the map to be persistent over time . if i pass only by pointer and then i try to call the object like this :

IGameObject* ComponentMadiator::getComponentByObjType(GameObjectType  gameObjectType)
{
    return componentsMap[gameObjectType];
}

the data in return object got lost.

Was it helpful?

Solution

Your problem is this function

void ComponentMadiator::Register(
    const GameObjectType gameObjectType,
    IGameObject*& gameObj)

It should probably accept non-reference

ComponentMadiator::Register(
      const GameObjectType gameObjectType, 
      IGameObject *object)

or accept a const reference to a pointer.

The underlying issue is that you can't decay a referece to a temporary to a non-const reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top