0

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.

user63898
  • 29,839
  • 85
  • 272
  • 514
  • any deeper reason for storing a `IGameObject*& gameObj` instead of only a pointer? – Najzero Mar 06 '14 at 07:33
  • the object itself will be called from the cache and keep persistent memory , when i try to call the object somewhere in teh application i loss all its data , i need it to be persistent – user63898 Mar 06 '14 at 08:12
  • You cannot convert a `WinFrameObj*` to an `IGameObject*&` for the same reason that [you cannot convert a `Derived**` to a `Base**`](http://stackoverflow.com/questions/8026040/). – fredoverflow Mar 06 '14 at 09:28
  • yes thanks , i ended to create new Register signature for each type. but when i try to get the pointer with getComponentByObjType i still get valid object without data , like its new object or something – user63898 Mar 06 '14 at 09:37
  • the strange thing is , that when i call the method getComponentByObjType right after i Register the object its working BUT when i call it from somewhere else in the app i loss the data – user63898 Mar 06 '14 at 09:41

1 Answers1

1

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.

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • i can't get pointer it losses the object store over the life of the aopplication – user63898 Mar 06 '14 at 08:11
  • @user63898 No, the parameter type won't affect anything like that. You've misunderstood something about pointers, references, and object lifetimes, but it's difficult to guess what. You should probably post a new question about the problem you're trying to solve instead of why your proposed solution doesn't work. – molbdnilo Mar 06 '14 at 08:34
  • Hmm , the main problem i try to solve is that the objects are somehow loosing the data i set them , somewhere they going out of scop – user63898 Mar 06 '14 at 08:43