Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
属性数组? 我在C++中制作控制台RPG,我有个问题。我可以制作一个数组来存储角色的属性吗?这些属性将被更改为“属性[atr\U health]+=200;”(我从一个属性更改为“英雄.Attribute[atr\U HITPOINTS]+=1;”的游戏中得到了这个想法?这个代码是否有效“属性[atr\u health]=damage;”?_C++_Arrays_Console - Fatal编程技术网

属性数组? 我在C++中制作控制台RPG,我有个问题。我可以制作一个数组来存储角色的属性吗?这些属性将被更改为“属性[atr\U health]+=200;”(我从一个属性更改为“英雄.Attribute[atr\U HITPOINTS]+=1;”的游戏中得到了这个想法?这个代码是否有效“属性[atr\u health]=damage;”?

属性数组? 我在C++中制作控制台RPG,我有个问题。我可以制作一个数组来存储角色的属性吗?这些属性将被更改为“属性[atr\U health]+=200;”(我从一个属性更改为“英雄.Attribute[atr\U HITPOINTS]+=1;”的游戏中得到了这个想法?这个代码是否有效“属性[atr\u health]=damage;”?,c++,arrays,console,C++,Arrays,Console,我会使用映射或无序映射和枚举来获取其值: #include <map> #include <string> enum Attrs {DAMAGE, LIFE, ARMOUR}; class Dude { private: map<int, int> attrs; //You can use a string map instead //map<string, int> attrs; public:

我会使用
映射或
无序映射和
枚举来获取其值:

#include <map>
#include <string>

enum Attrs {DAMAGE, LIFE, ARMOUR};


class Dude
{
    private:
     map<int, int> attrs;
    //You can use a string map instead
    //map<string, int> attrs;
    public:
    Dude()
    {
        attrs[DAMAGE]   = 10;
        //attrs["DAMAGE"]   = 10;
        attrs[LIFE]     = 10;
        //attrs["LIFE"]     = 10;
        attrs[ARMOUR]   = 5;
        //attrs["ARMOUR"]   = 10;
    }
}
#包括
#包括
枚举属性{伤害、生命、盔甲};
同班同学
{
私人:
地图属性;
//您可以改为使用字符串映射
//地图属性;
公众:
杜德()
{
属性[伤害]=10;
//属性[“损害”]=10;
属性[寿命]=10;
//属性[“生命”]=10;
属性[装甲]=5;
//属性[“装甲”]=10;
}
}

类似的方法可能会奏效。

您应该阅读有关类/结构的内容

类是将各种相关值捆绑在一个对象中并提供对这些值的抽象访问的干净方法

在您的案例中,这可能是一个示例:

enum class Attribute
{
   Health, Dexterity
}

class Character
{
    std::map<Attribute, unsigned int> attributes;

public:

   unsigned int GetAttribute(Attribute type) { return attributes[type]; }

   unsigned int GetHealth() { return GetAttribute(Attribute::Health); }
   unsigned int GetDexterity() { return GetAttribute(Attribute::Dexterity); }
}
在我只实现getter的示例中,setter可以如下所示:

Character myCharacter;

unsigned int health = myCharacter.GetHealth(); // usign the concrete getter
unsigned int dexterity = myCharacter.GetAttribute(Attribute::Dexterity); // using the generic getter
void SetAttribute(Attribute type, unsigned int value)
{
    attributes[type] = value;
}

更高级的实现可以在
attributes
类中抽象这些值(属性)集(在这种情况下,我们只需要重命名类,因为它不包含属性以外的任何字符相关信息)。这种方法的优点是,该类可以重用,例如用于具有boni属性的武器。缺点是访问稍微繁琐。

字符类如何?
映射属性,而不是
映射属性。。。另外,由于
enum
仅在类
Dude
内有意义,因此应将其移动到类的公共部分。如果键是一组简单的连续零基整数,则数组将更简单、更有效。如果您需要对更复杂的密钥类型的支持,请使用映射。@StefanoSanfilippo,您对映射的理解是正确的,但我认为让enum私有化一点也不重要,也许他需要在主游戏代码的其他部分使用它。@DanielSanchez事实上,我说的是“public”,而不是“private”:@MikeSeymour,是的,它会更有效,这只是我的意见。我在游戏中使用了地图,当你试图从文件中加载内容时,使用字符串访问是很有帮助的。反正也是我的观点:)