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

时间:2015-11-27 22:20:44

标签: c++ inheritance

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

问题是 - 对于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...
};

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

1 个答案:

答案 0 :(得分:0)

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

我鼓励您熟悉design patterns。它们为回归编程困境和问题提供了很好的解决方案。

在你的情况下,像composite pattern这样的东西可能是实用的。

另外,也许某些类不需要直接使用GameObjects?也许你可以在不同的模块中分离你的逻辑?

希望这有帮助!