简体   繁体   English

指向向量的变量

[英]Variable to Point to a Vector

I'm attempting to make a command line side scrolling shooter. 我正在尝试制作命令行侧滚动射击游戏。 However, I am struggling to get the inheritance and class/struct structure set up for the different ship types. 但是,我在努力为不同的船型设置继承和类/结构结构。 However, there is a fair chance that I am completely misunderstanding how to go about this. 但是,我完全有可能完全误解该如何做。

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:

    }
}
};

That's the Ship class as it stands currently. 这就是目前的Ship类。 My idea was that I will be able to pass the parameters to the createShip method (from my gameloop) and that will create a new Ship of the correct type and then dump it into the list of ships (which is global). 我的想法是,我将能够将参数传递给createShip方法(来自我的游戏循环),这将创建正确类型的新Ship ,然后将其转储到Ships列表(全局)中。

However, whilst I can easily access the attributes of my newShip object that belong to whichever struct has been used to create it, in the case shown I can use newShip.speed after newShip = Ship::enemyShip newShip; 然而,尽管我可以很容易地访问我的属性newShip属于哪个对象struct已被用来创建它,在显示我可以使用的情况下newShip.speednewShip = Ship::enemyShip newShip; to access these. 访问这些。 However, I cannot work out how to access all of the attributes that it will have inherited from the parent class; 但是,我无法解决如何访问它将从父类继承的所有属性的问题。 such as health , charging , etc. 例如healthcharging等。

Any help would be appreciated, I have searched, however, most answers simply say this->x = x::x or something of that ilk, and I have tried newShip->health = 100 and that doesn't work. 任何帮助将不胜感激,我已经搜索过,但是,大多数答案只是说this->x = x::x或类似的东西,而我尝试过newShip->health = 100却不起作用。 Consequently, either a little more detail would be appreciated or a completely different answer. 因此,将不胜感激,或者提供了完全不同的答案。

Thanks in advance! 提前致谢!

You need to hoist (for example) enemyShip out of Ship and declare it as: 您需要从Ship吊起(例如) enemyShip Ship并将其声明为:

struct enemyShip : Ship
{
    int speed;
    shipType type;
};

The : Ship says that enemyShip derives from Ship . The : ShipenemyShipShip派生的。

You will then need to define createShip outside struct Ship (because inside Ship enemyShip won't be properly defined). 然后,您将需要在struct Ship外部定义createShip (因为在Ship enemyShip内部将无法正确定义)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM