nco*_*org 0 c++ inheritance templates
是否可以使用子类作为模板参数继承一个类?
template<typename T>
struct A{
T t;
};
struct B:A<B>{
unsigned u;
};
int main(){
B b;
}
Run Code Online (Sandbox Code Playgroud)
是的,你可以做到.
你的问题似乎是错误‘A<T>::t’ has incomplete type.例如,请参见此处.
如果你
struct B;
template<typename T>
struct A{
T *t;
};
struct B : A<B>{
unsigned u;
};
int main(){
B b;
}
Run Code Online (Sandbox Code Playgroud)