![](/img/trans.png)
[英]C++: Can I cast a vector <derived_class> to a vector <base_class> during a function call?
[英]How can I cast a pointer to call a derived class function/method? C++
在基于netback的无赖类游戏中,我有一个带有名为Creature的派生类的基本Entity 。 我也有另外一个派生类生物的叫球员。
由于Player基本上是Creature,但具有添加/修改的方法,因此我的攻击方法(针对这两者)都需要2个指向Creature对象的指针。 一种是被攻击的生物 ,另一种是玩家。
在播放器版本的虚拟方法攻击中,我需要设置播放器的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’
我假设是因为它是Creature类型的。但是我无法将其强制转换为可以实际使用播放器上的方法。
我还注意到在Creature中定义了“ setXp”和“ getXp” 。 但是Creature没有xp。我可以/应该将计分方法添加到生物并在Player中定义它们吗?
我怎样才能做到这一点? 谢谢! 这是一些代码。 让我知道您是否需要更多:
#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
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;
}
#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
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
}
}
线
player = static_cast<Player*>(player);
实际上什么也没做。 您仍然具有相同Creature*
类型的相同player
变量(因为使用此类型声明了该变量)。 相反,你可以写
Player* player_ref = reinterpret_cast<Player*>(player);
然后使用player_ref -> setScore();
。
不过,多态解决方案会更整洁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.