简体   繁体   English

游戏中的C ++指针

[英]C++ Pointers In A Game

I am in what seems to be a never ending quest to understand pointers. 我似乎永远无法理解指针。 I finally have a working code using pointers to create a quick character and enemy, then mock an overly simple battle. 我终于有了一个使用指针的工作代码,以创建一个快速的角色和敌人,然后模拟一个过于简单的战斗。 It works 100% as it seems, but I'm not 100% sure it's correct or what I did to make it right. 它似乎可以100%正常运行,但是我不确定100%正确或我做对了。 I have read 13 chapters so far in my C++ book (2 being over pointers specifically), but I'm still not sure it's clicked. 到目前为止,我已经在我的C ++书中阅读了13章(其中2章专门针对指针),但是我仍然不确定它是否被单击。 If this looks like a valid use and proper utilization of them, I think I'm getting there, just wanted some clarification if it is. 如果这看起来像是对它们的有效使用和正确利用,我想我已经到达了,只是想澄清一下。 Thanks in advance! 提前致谢!

#include <iostream>
#include <string>

#ifndef CLASS_H
#define CLASS_H

class Unit
{
public:
    Unit(const std::string name, int hp, int power);
    ~Unit();

    void    printHP();
    void    attack(Unit* unit);
    bool    isDead();

private:
    std::string     mName;
    int             mHP;
    int             mPower;

};

Unit::Unit(const std::string name, int hp, int power)
{
    mName   = name;
    mHP     = hp;
    mPower  = power;
}
void Unit::printHP()
{
    std::cout << std::endl;
    std::cout << mName << "'s HP: " << mHP << std::endl;
}
void Unit::attack(Unit* unit)
{
    std::cout << std::endl;
    std::cout << unit->mName << " takes " << this->mPower << " damage! " << std::endl;
    unit->mHP -= this->mPower;
}
bool Unit::isDead()
{
    return this->mHP < 1;
}
Unit::~Unit()
{

}
#endif

int main()
{
    Unit player1("GoodFellow", 20, 5);
    Unit Enemy("ABadMan", 10, 2);

    while (!Enemy.isDead())
    {
        player1.printHP();
        Enemy.printHP();

        player1.attack(&Enemy);
        Enemy.attack(&player1);
    }
}

Modified it to this... I think I get the reason for only using a reference, just trying to get the concept of pointers.... 修改为此...我想我得到仅使用引用的原因,只是试图获得指针的概念。

#include <iostream>
#include <string>

#ifndef CLASS_H
#define CLASS_H

class Unit
{
public:
    Unit(const std::string name, int hp, int power);
    ~Unit();

    void    printHP();
    void    attack(Unit& unit);
    bool    isDead();

    void    setHP(int hp);

private:
    std::string     mName;
    int             mHP;
    int             mPower;

};

Unit::Unit(const std::string name, int hp, int power)
{
    mName   = name;
    mHP     = hp;
    mPower  = power;
}
void Unit::printHP()
{
    std::cout << std::endl;
    std::cout << mName << "'s HP: " << mHP << std::endl;
}
void Unit::attack(Unit& unit)
{
    std::cout << std::endl;
    std::cout << unit.mName << " takes " << this->mPower << " damage! " << std::endl;
    unit.mHP -= this->mPower;
}
bool Unit::isDead()
{
    return this->mHP < 1;
}
void Unit::setHP(int hp)
{
    this->mHP = hp;
}
Unit::~Unit()
{

}
#endif

int main()
{
    Unit player1("GoodFellow", 20, 5);
    Unit Enemy("ABadMan", 10, 2);

    while (true)
    {
        while (!Enemy.isDead())
        {
            player1.printHP();
            Enemy.printHP();

            player1.attack(Enemy);
            Enemy.attack(player1);
        }

        char playAgain;
        std::cout << "Fight again? (y)/(n)" << std::endl;
        std::cin >> playAgain;

        if (playAgain == 'n')
        {
            break;
        }
        else
        {
            Enemy.setHP(10);
        }

    }
}

Yes, that's a reasonable use of pointers. 是的,这是对指针的合理使用。 I was actually glad to see that you used them very little. 实际上,我很高兴看到您很少使用它们。 I was half expecting to see Unit* player1 = new Unit ... and such all over the place. 我一半期望看到Unit* player1 = new Unit ...都是这样。 But no, you used automatic storage duration all over (rather than dynamic), which was very appropriate. 但是,不,您使用了自动存储持续时间(而不是动态),这非常合适。

So the main reason for passing Unit* s to the attack functions is to that inside the function you can modify the Unit that you're attacking and have the effect seen outside. 因此,将Unit*传递给attack功能的主要原因是在该功能内部,您可以修改要攻击的Unit并在外部看到效果。 If you were to pass the Unit s directly, they would be copied into the function and then the unit->mHP -= this->mPower; 如果要直接传递Unit ,则将它们复制到函数中,然后复制到unit->mHP -= this->mPower; would only affect the copy. 只会影响副本。

However, there is a more appropriate tool at your disposal here. 但是,这里有一个更合适的工具可供您使用。 You can use references . 您可以使用引用 A reference also allows you to pass an object without copying it, so that modifications inside the function can be seen outside. 引用还允许您传递对象而不复制它,以便可以在外部看到函数内部的修改。 Your attack function signature would change to: 您的攻击功能签名将变为:

void Unit::attack(Unit& unit)

The type Unit& is a "reference to Unit ". Unit&类型是“对Unit引用”。 Don't confuse the use of & with taking the address of an object - it means something completely different here. 不要将&的使用&对象的地址混为一谈-这意味着这里的东西完全不同。 You would then call it like so: 然后,您可以这样称呼它:

player1.attack(Enemy);

The point is you should try to avoid pointers as much as possible. 关键是您应尽量避免使用指针。 Since you can use references here, which are safer (you do not need to check for null ), you should use them. 由于您可以在此处使用更安全的引用(无需检查null ),因此应使用它们。

It's fine to learn about how pointers to work, but in doing so, you should learn how to use other more appropriate tools for the job. 了解指针如何工作是很好的,但是在这样做时,您应该学习如何使用其他更合适的工具来完成工作。

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

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