簡體   English   中英

C ++繼承,發送指向基類的指針

[英]C++ inheritance, sending a pointer to base class

我有一個從Creep類派生的ninjaCreep類。 我想將通過派生類的參數獲取的指針傳遞給基類的構造函數,但是出現此錯誤:

../ 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);

您只需要使用原始的參數即可:

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.  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM