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

时间:2015-03-15 19:34:01

标签: c++ pointers inheritance constructor superclass

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

  

../ ninjacreep.cpp | 4 |错误:'operator *'不匹配(操作数类型为>'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
}

之前我从未传递过指向基类的指针,所以我认为错误存在于那里?

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

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

1 个答案:

答案 0 :(得分:2)

您只需使用原来的参数:

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.