【发布时间】:2026-02-14 09:15:02
【问题描述】:
让 B 类成为 A 的基础:
class B
{
public:
std::tuple<int, bool, float> properties.
}
class A : public B
{
public:
std::tuple<float, std::string, std::string> derivedProperties.
}
有没有办法将派生属性元组添加到基础属性元组?例如通过某种形式的 CRTP?我知道基类和派生类型的属性在编译时是已知的,但我似乎无法弄清楚如何组合不同继承级别的属性。
【问题讨论】:
-
至于你的问题,你可能有兴趣了解template parameter packs。
-
但是如何使用可变参数模板构造函数来确定基本成员元组的类型?
-
B中的元组是否应该始终至少具有您显示的类型?并且您想添加更多字段(因此生成的properties元组将是std::tuple<int, bool, float, float, std::string, std::string>? -
另外,您是否考虑过使用
struct代替元组?因为派生类A可以有自己的struct继承自基类。 -
对外界来说是的,我希望 B 类中的元组具有所有属性类型。
标签: c++ inheritance tuples