0

编辑

  1. 问题出在GoFish.h文件中,具体来说是在构造函数中,它试图实例化玩家对象。

  2. 编译器抛出以下错误消息:“Player”中没有名为“noOfBooks”的成员

GoFish() {players = new GoFishPlayer[2];} // Instantiate two players

对于初学者来说,对象切片似乎是 OOP 中最模糊的概念之一。我一直在用 C++ 开发这款纸牌游戏,我有一个名为Player的基类和一个名为GoFishPlayer的派生类。当尝试访问引用回 Player 对象的 GoFishPlayer 对象的方法时,程序倾向于切掉派生类的特定方法和属性,从而使其成为基对象的克隆。有没有办法克服这个问题?

游戏.h

抽象类 Game :它构成了这两个游戏的基础 - GoFish 和 CrazyEights

class Game {

protected:
Deck* deck;
Player* players;
int player_id;

public:
Game(){
    deck = Deck::get_DeckInstance(); // Get Singleton instance
    player_id = choosePlayer();
    players = NULL;
}
....
}

GoFish.h

派生类 GoFish - 当我尝试实例化从游戏类派生的 Player 对象时,问题出现在构造函数中

class GoFish : public Game{

static GoFish* goFish;
GoFish() {players = new GoFishPlayer[2];} // Instantiate two players

public:
static GoFish* get_GoFishInstance() {
    if(goFish == NULL)
        goFish = new GoFish();

    return goFish;
}

播放器.h

class Player{

protected:
std::string playerName;
Hand hand;
bool win;

public:
Player(){ 
    playerName = "Computer"; // Sets default AI name to Computer
    hand = Hand(); // Instatiate the hand object
    win = false;
}
....

GoFishPlayer.h

class GoFishPlayer : public Player {

private:
std::vector <int> books;
int no_of_books;

public:
GoFishPlayer() {
    no_of_books = 0;
    books.resize(13);
}

int noOfBooks(){return no_of_books;}
void booksScored() {no_of_books++;}

bool checkHand() {}
....
4

1 回答 1

1

你的问题的措辞对我来说似乎模棱两可,但据我所知,你正试图GoFishPlayer通过对对象的引用来访问方法Player?这不是对象切片造成的问题,而是多态性的工作原理。

您需要转换对象的引用Player,使其成为对GoFishPlayer对象的引用。

class Parent
{
    public:
        void foo() { std::cout << "I'm a parent" << std::endl; }
};

class Derived : public Parent
{
    public:
        void bar() { std::cout << "I'm a derived" << std::endl; }
};


int main()
{
    Derived d;

    // reference to a derived class stored as a prent reference
    // you can't access derived methods through this
    Parent& p_ref = d;
    // this won't work
    // p_ref.bar();

    Derived& d_ref = static_cast<Derived&>(p_ref);
    // this works
    d_ref.bar();
}

这仅在您确定它p_ref实际上是 typeDerived或者它是继承自 的类型时才有效Derived。如果您不能确定是否需要使用dynamic_cast然后捕获任何std::bad_cast抛出的异常进行运行时检查。

于 2016-03-28T17:03:57.247 回答