C++ 当函数 arg 类型为类接口时,无法传递对指针的引用
C++ Can't pass reference to a pointer when the function arg type is class interface
我的接口看起来像这样:
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
}
...
...
}
现在我在代码中做在主要类中
WinFrameObj* m_pMainWindowFrameObjCenter ; // defined in the header as memeber
pMainWindowFrameObjCenter = WinFrameObj::createObj();
ComponentMadiator::Instance().Register(MAIN_WIN_FRAME,pMainWindowFrameObjCenter);
我遇到此错误:
error C2664: 'ComponentMadiator::Register' : cannot convert parameter 2 from 'WinFrameObj *' to 'IGameObject *&'
我需要componentMadiator ::注册方法是通用的。
更新
我这样做的原因是将我存储在地图中的数据随着时间的推移而持久。如果我只通过指针通过,然后尝试这样调用对象:
IGameObject* ComponentMadiator::getComponentByObjType(GameObjectType gameObjectType)
{
return componentsMap[gameObjectType];
}
返回对象中的数据丢失了。
您的问题是此功能
void ComponentMadiator::Register(
const GameObjectType gameObjectType,
IGameObject*& gameObj)
它可能应该接受非参考
ComponentMadiator::Register(
const GameObjectType gameObjectType,
IGameObject *object)
或接受指针的const引用。
潜在的问题是,您不能将转介到临时引用到非const的引用。
相关文章:
- C++取消引用指针.为什么会发生变化
- 深层复制具有自引用指针的类
- Visual c ++,使用字符串引用/指针调用 dll 函数
- std::unordered_map::提取引用/指针失效
- 为什么在引用指针时将 const 放在 & 符号的左侧有效,而在右侧则无效?
- 区分接受常量参数的函数引用/指针和与函数参数同名的非常量参数
- 如何在 c++ 中使用带有数学运算的引用/指针?
- 了解通过引用传递取消引用指针时C++堆/堆栈分配
- 取消引用指针并立即为其分配变量,导致分段错误
- 如何获取指向类(而不是对象)的引用/指针
- 将类型参数传递给自引用指针
- 我应该如何定义返回指针的函数?(引用指针与指针指针)
- 在析构函数内取消引用指针时出现分段错误
- 来自引用指针的内存泄漏
- 引用指针后面的值
- 无法取消引用指针
- 引用指针调用成员函数
- 未定义的引用指针的变量模板在clang中功能,而不是GCC
- 取消引用指针以创建数组的副本
- 有关启动引用指针的引用的问题