重写复杂继承层次结构的每个类的初始化列表
Rewriting initialization list for each class of complex inheritance hierarchy
我想为游戏构建复杂的类层次结构(在本例中)。
问题是,对于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?也许你可以把你的逻辑分成不同的模块?
希望这能有所帮助!
相关文章:
- C++类 - 初始化列表 - 递归 - 按值传递
- 在初始化列表之外手动调用基类的构造函数
- C++:带有大括号初始化列表的函数调用表达式 - 标准是否规定在单个元素列表的微不足道的情况下忽略大括号?
- std::map与谓词与初始化列表
- 类内初始化与构造函数初始化列表的顺序
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 如何在初始化列表中的构造函数之后初始化变量/对象?
- C++初始化列表与分配值
- C++初始化列表中的向量集大小或调整大小
- 在构造函数初始化列表中使用 std::variant
- emplace_back初始化列表错误,当初始化列表在独立变量上工作时
- 解释了构造函数成员初始化列表
- 使用初始化列表时如何获取私有数据?
- 用初始化列表和超类构造函数声明子类构造函数的正确方式
- 如何在成员初始化列表中声明共享指针
- 庞大的初始化列表,如何修复"fatal error C1060: compiler is out of heap space"
- 我可以检查初始化列表中设置的构造函数主体中的变量吗
- 使用整数初始化列表初始化长双精度的向量
- 是否可以在C++中使用初始化列表设置数组的特定成员?