我想要做:
template <class Derived=BattleData>
class BattleData : public BattleCommandManager<Derived> {
};
但显然BattleData
没有声明,所以我尝试了前向声明:
template <class T> class BattleData;
template <class Derived=BattleData>
class BattleData : public BattleCommandManager<Derived> {
};
但后来我得到
错误:“第二行模板参数的数量错误,带有 BattleData。
我真的看不到解决方案!
编辑:
我这样做的原因是因为我希望能够BattleData
直接用作 a class
,但我也希望能够对其进行子类化,在这种情况下,我必须将派生指定class
为第二个template
参数。
例如,假设我BattleData
班的语料库是:
template <class Derived> class BattleData: public BaseClass<Derived> {
void foo1(){};
void foo2(){};
void foo3(){};
}
我有一个子类
template class SubBattleData: public BattleData<SubBattleData> {
void foo1(){};
}
在某些情况下,我仍然希望能够编写如下代码:
BattleData *x = new BattleData(...);
如果不能使用默认参数,我什至无法执行以下操作:
BattleData<BattleData> *x = new BattleData<BattleData>(...);
一方面,BattleData 类中没有虚拟化函数的原因是没有虚拟函数的好处。它对我不起作用的另一个原因是,只有当它们存在于派生类型中时,父 CRTP 类之一才会调用函数(使用decltype(Derived::function)
和 enable-if 类似的结构),否则会回退到默认行为。由于可能存在大量具有特定设计模式的函数(例如 CRTP,它读取具有许多不同案例的协议并仅在派生类指定相应函数时以特定方式处理案例,否则只需传输它而不进行处理)。
所以这些函数可以存在也可以不存在SubBattleData
,BattleData
但是如果实例化,这两个类都可以正常工作,但不可能实例化BattleData
。