gpt4 book ai didi

游戏中的 C++ 指针

转载 作者:行者123 更新时间:2023-11-30 01:55:19 25 4
gpt4 key购买 nike

我正处在一个似乎永无止境的理解指针的探索中。我终于有了一个使用指针创建快速角色和敌人的工作代码,然后模拟了一场过于简单的战斗。它看起来 100% 有效,但我不能 100% 确定它是正确的,也不能 100% 确定我做了什么使它正确。到目前为止,我已经在我的 C++ 书中阅读了 13 章(其中 2 章专门针对指针),但我仍然不确定它是否被点击了。如果这看起来像是对它们的有效使用和正确利用,我想我已经到了,只是想澄清一下。提前致谢!

#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);
}
}

修改成这样...我想我明白了只使用引用的原因,只是想了解指针的概念....

#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);
}

}
}

最佳答案

是的,这是对指针的合理使用。我真的很高兴看到你很少使用它们。我有一半希望看到 Unit* player1 = new Unit ... 等等。但是不,你全程使用自动存储持续时间(而不是动态的),这是非常合适的。

所以将 Unit* 传递给 attack 函数的主要原因是在函数内部你可以修改你的 Unit '攻击并产生外面看到的效果。如果您直接传递 Unit,它们将被复制到函数中,然后 unit->mHP -= this->mPower; 只会影响复制.

但是,这里有一个更合适的工具供您使用。您可以使用引用。引用还允许您在不复制对象的情况下传递对象,以便函数内部的修改可以在外部看到。您的攻击函数签名将更改为:

void Unit::attack(Unit& unit)

Unit& 类型是“对Unit 的引用”。不要将 & 的使用与获取对象的地址混淆 - 它在这里意味着完全不同的东西。然后你会这样调用它:

player1.attack(Enemy);

重点是您应该尽量避免使用指针。由于您可以在此处使用更安全的引用(您不需要检查 null),因此您应该使用它们。

了解指针的工作原理很好,但在此过程中,您应该学习如何使用其他更合适的工具来完成这项工作。

关于游戏中的 C++ 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20860421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com