如何转换指针以调用派生类函数/方法? C ++

时间:2013-05-04 19:26:22

标签: c++ class pointers casting

在我基于netback的流氓式游戏中,我有一个基础实体,带有一个名为 Creature的派生类。我还有另一个派生类生物名为玩家。

由于播放器基本上是一个生物,但是使用添加/修改的方法,我的攻击方法(两者都需要)指向生物对象的2个指针。一个用于生物受到攻击,一个用于玩家。

在虚拟方法的玩家版本中,我需要设置玩家的XP和Score成员变量以反映战斗。我一直在说错误:

Player.cpp:34:11: error: ‘class Creature’ has no member named ‘setScore’
Player.cpp:34:29: error: ‘class Creature’ has no member named ‘getScore’

我假设是因为它是生物类型..但是我无法投射它以便我可以实际使用播放器上的方法。

我还注意到我在生物中定义了“setXp”和“getXp”。生物的没有xp ..可能/应该我只是添加对生物进行评分并在玩家中定义它们?

我怎样才能做到这一点?谢谢!这是一些代码。如果您需要更多信息,请与我们联系:

Player.h

#ifndef _Player_included_
#define _Player_included_

#include "Creature.h"
#include "Weapon.h"
#include "Armor.h"

class Player : public Creature {

public:
    Player(void);
    virtual ~Player(void);

    void dumpStats();
    virtual bool move(char dir, DungeonLevel & dl, std::mt19937 & randomGen$
    virtual bool canAttack(DungeonLevel & dl);
    int getHit(std::mt19937 & randomGen);
    bool fightOrFlight(DungeonLevel & dl, std::mt19937 & randomGen);
    virtual void attack(Creature * monster, Creature * player, std::mt19937$

    void generateHP();
    void addXp(int xpToAdd);
    void addScore(int xpToMultiply); //score is just 2x the xp.
    virtual int getScore();
    virtual void setScore(int scoreToSet);
    virtual int getXp();
    virtual void setXp(int xpToSet);

    void setXPToLevel(int xpToLevelToSet);
    int getXPToLevel();
private:
    //Creature provides level, HP, and maxHP
    int score;
    int xp;
    int xpToLevel;
    Weapon * playerWeapon;
    Armor * playerArmor;
};

#endif

Player.cpp的攻击方法

void Player::attack(Creature * monster, Creature * player, std::mt19937 & randomGen, Du$
    int monsterHit = monster->getHit(randomGen);
    int playerHit = getHit(randomGen);

    player = static_cast<Player*>(player);

    if ((monster->getHP() - playerHit) <= 0){
            playerHit = monster->getHP();
            player->addXp(playerHit);
            player->setScore((player->getScore()) + (2 * playerHit)));
            cout << "Monster name: " << monster->getName() << endl;
            cout << "monster killed: " << monster << endl;
            monster->removeMonster(dl);
    }
    else if (monster != NULL){
            cout << "Monsters Hit: " << monsterHit << endl;
            player->setHP((player->getHP()) - monsterHit);
            player->addXp(playerHit);
            player->setScore((player->getScore()) + (2 * playerHit)));
            monster->setHP((monster->getHP() - playerHit));
            cout << "Your HP: [" << player->getHP() << "]/[" << player->getMaxHP() $
            cout << "Monsters HP: [" << monster->getHP() << "]/[" << monster->getMa$
    }
    else if ((player->getHP() - monsterHit) <= 0){
            monsterHit = player->getHP();
            //game over
    }

    cout << "You hit: " << playerHit << endl;
}

Creature.h

#ifndef _Creature_included_
#define _Creature_included_

#include "Entity.h"
#include "DungeonLevel.h"

#include <random>

class Creature : public Entity {

public:
    Creature(void);
    virtual ~Creature(void);

    virtual void dumpObject();
    virtual void dumpObjectData();
    virtual void writeFragment(std::ostream & output);
    virtual void writeDataAsFragment(std::ostream & output);
    virtual void setElementData(std::string elementName, std::string elementValue);

    virtual bool move( DungeonLevel & dl, Creature & player, std::mt19937 & randomGen);
    virtual void attack(Creature * monster, Creature & player, std::mt19937 & randomGen, DungeonLevel & dl);
    virtual int getHit(std::mt19937 & randomGen);

    virtual bool canAttack();
    virtual void removeMonster(DungeonLevel & dl);

    virtual void setXLoc(int xToSet);
    virtual int getXLoc();
    virtual void setYLoc(int yToSet);
    virtual int getYLoc();

    virtual void setXp(int xpToSet);
    virtual int getXp();
    virtual void addXp(int xpToAdd);

    virtual int getLevel();
    virtual void setLevel(int levelToSet);
    virtual int getHP();
    virtual void setHP(int HPToSet);
    virtual int getMaxHP();
    virtual void setMaxHP(int maxHPToSet);

private:
    int xLoc;
    int yLoc;
    int level;
    int HP;
};

#endif

Creature.cpp中的攻击方法

void Creature::attack(Creature * monster, Creature & player, std::mt19937 & randomGen,  DungeonLevel & dl){
    int monsterHit = monster->getHit(randomGen);
    int playerHit = player.getHit(randomGen);

    if ((monster->getHP() - playerHit) <= 0){
            playerHit = monster->getHP();
            cout << "Monster name: " << monster->getName() << endl;
            this->removeMonster(dl);
            cout << "back to creature attack with monster removed.."<<endl; 
            cout << "delete monster here." << endl;
    }
    else if ((player.getHP() - monsterHit) <= 0){
            cout << "You died. Game Over." << endl;
            //make a function to end the game
    }
}

1 个答案:

答案 0 :(得分:1)

该行

player = static_cast<Player*>(player);

实际上什么也没做。您仍然具有相同player类型的相同Creature*变量(因为它是使用此类型声明的)。相反,你可以写

Player* player_ref = reinterpret_cast<Player*>(player);然后使用player_ref -> setScore();

多态解决方案虽然更整洁。