我正在尝试制作命令行侧滚动射击游戏。但是,我正在努力为不同的船型设置继承和类/结构结构。但是,很有可能我完全误解了如何去做。
class Ship
{
int health;
float charging;
bool charged;
coords location;
bool shielded;
int chargeSpeed;
int cannons;
int shieldHealth;
struct enemyShip
{
int speed;
shipType type;
};
struct bossShip
{
int Bonuspoints;
};
struct myShip
{
int lives;
int points;
int isDead;
};
void createShip(shipType type, int speed, int health, bool shields, int cannons, int chargeSpeed, int bonusPoints)
{
switch (type)
{
speeder:
Ship::enemyShip newShip;
newShip.speed = 0;
ships.push_back(newShip);
bruiser:
sprayer:
boss:
}
}
};
这就是当前的Ship
类。我的想法是,我将能够将参数传递给 createShip 方法(来 self 的游戏循环),这将创建一个正确类型的新 Ship
,然后将其转储到船舶列表中(是全局性的)。
但是,虽然我可以轻松访问我的 newShip
对象的属性,这些属性属于用于创建它的任何 struct
,但在所示的情况下,我可以使用 newShip.speed
在 newShip = Ship::enemyShip newShip;
之后访问这些。但是,我不知道如何访问它将从父类继承的所有属性;比如健康
,充电
等
任何帮助将不胜感激,我已经搜索过,但是,大多数答案只是说 this->x = x::x
或类似的东西,我已经尝试过 newShip->health = 100
这不起作用。因此,要么提供更多细节,要么提供完全不同的答案。
提前致谢!
最佳答案
您需要将(例如)enemyShip
吊出 Ship
并将其声明为:
struct enemyShip : Ship
{
int speed;
shipType type;
};
: Ship
表示 enemyShip
派生自 Ship
。
然后您需要在结构 Ship
之外定义 createShip
(因为在 Ship
内部 enemyShip
不会正确定义)。
关于c++ - 指向 vector 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34809324/