C++ 继承,发送指向基址的指针 class

C++ inheritance, sending a pointer to base class

我有一个 ninjaCreep class 派生自 class Creep。我想将我通过派生 class 的参数获取的指针传递给基础 class' 构造函数但是我收到此错误:

../ninjacreep.cpp|4|error: no match for ‘operator*’ (operand type is >‘Ogre::SceneManager’)|

代码:

ninjaCreep::ninjaCreep(Ogre::SceneManager& sceneManager, int x, int y, int z, std::string id)
        : Creep(*sceneManager, x, y ,z, id) //line 4
{
    //ctor
}

我以前从未将指针传递给基数 class,所以我认为错误出在某处?

Creep 构造函数具有与 ninjaCreep 相同的参数:

Creep(Ogre::SceneManager& sceneManager, int x, int y, int z, std::string id);

您只需按原样使用参数:

ninjaCreep::ninjaCreep(Ogre::SceneManager& sceneManager, int x, int y, int z, std::string id)
        : Creep(sceneManager, x, y ,z, id) //line 4 no "*"
{
    //ctor
}

sceneManager 不是指针:它是对类型 SceneManger 对象的 引用 。它被用作普通的 SceneManager 对象,没有任何取消引用。

重要提示:

& 可以是类型声明的一部分:

int a 
int &i=a ;  // i is a reference.  you can then use i and a interchangeably

不要与地址获取运算符混淆:

int a; 
int *pa = &a;  // pa is a pointer to a.  It contains the adress of a. 
               // You can then use *pa and a interchangeably
               // until another address is assigned to pa.