重写复杂继承层次结构的每个类的初始化列表

Rewriting initialization list for each class of complex inheritance hierarchy

本文关键字:初始化 列表 继承 层次结构 重写 复杂      更新时间:2023-10-16

我想为游戏构建复杂的类层次结构(在本例中)。

问题是,对于3-4个层次来说,这不是问题,但当我有10-20个层次时,这就变成了一种真正的痛苦。有了所有这些构造函数参数,以及初始化列表中的基类构造函数调用(大多数情况下,每个类+当前类成员都有相同的参数),就不鼓励编写代码了。

class gameObject
{
public:
    gameObject(const std::string& _name, const size_t& _id)
        : name(_name), id(_id) {/* nothing here */}
    virtual ~gameObject() = 0;
private:
    const std::string name;
    const size_t id;
    //...
};
class item : public gameObject
{
public:
    item(const std::string& _name, const size_t& _id, const unsigned short& _item_lvl, 
         const unsigned short& _required_lvl, const float& _weight, const unsigned& _base_value)
        : gameObject(_name, _id), item_lvl(_item_lvl), required_lvl(_required_lvl), 
          weight(_weight), base_value(_base_value) {/* nothing here */}
private:
    const unsigned short item_lvl;
    const unsigned short required_lvl;
    const float weight;
    const unsigned base_value;
    //...
};
class equipment : public item
{
public:
    equipment(const std::string& _name, const size_t& _id, const unsigned short& _item_lvl, 
              const unsigned short& _required_lvl, const float& _weight, const unsigned& _base_value,
              const std::vector<char>& _allowed_prof, const std::vector<int>& _required_perks, 
              const std::vector<int>& _item_stats,  const short& _item_position, 
              const unsigned short& _efficiency_lvl,
              const std::string& _upgrade_name)
        : item(_name, _id, _item_lvl, _required_lvl, _weight, _base_value), allowed_prof(_allowed_prof), 
          required_perks(_required_perks), item_stats(_item_stats), item_position(_item_position),
          efficiency_lvl(_efficiency_lvl), upgrade_name(_upgrade_name) {/* nothing here */}
private:
    const std::vector<char> allowed_prof;
    const std::vector<int> required_perks;
    const std::vector<int> item_stats;
    const short item_position;
    unsigned short efficiency_lvl;
    std::string upgrade_name;
    //...
    /* tons of other members */
};
class weapon : public equipment
{
public:
    weapon(const std::string& _name, const size_t& _id, const unsigned short& _item_lvl, 
           const unsigned short& _required_lvl, const float& _weight, const unsigned& _base_value,
           const std::vector<char>& _allowed_prof, const std::vector<int>& _required_perks, 
           const std::vector<int>& _item_stats,  const short& _item_position,
           const unsigned short& _efficiency_lvl, const std::string& _upgrade_name)
        : equipment(_name, _id, _item_lvl, _required_lvl, _weight, _base_value, _allowed_prof, 
                    _required_perks, _item_stats, _item_position, _efficiency_lvl, _upgrade_name) 
                    /* TONS OTHER */ { /* nothing here */ }
private:
    // TONS...
};
class melee_weapon : public weapon
{
    // TONS MORE...
};
class one_handed_weapon : public melee_weapon
{
    // MORE MORE TONS...
};
class one_handed_sword : public one_handed_weapon
{
    // MORE MORE MORE MORE MORE MORE MORE MORE MORE MORE...
};

有没有更好的方法来建立这样的层次结构?不要在所有构造函数中一遍又一遍地重写相同的东西?

继承是一个不错的工具,但有时它并不是解决问题的最佳方案。在你的情况下,它似乎被过度使用了。

我鼓励你熟悉设计模式。它们为反复出现的编程困境和问题提供了很好的解决方案。

在您的情况下,类似于复合模式的东西可能是实用的。

另外,也许有些类不需要直接成为GameObjects?也许你可以把你的逻辑分成不同的模块?

希望这能有所帮助!