代码可以通过编译吗?如果不能应该如何修改?
template<class T> class Foo{
T tVar;
public:
Foo(T t) : tVar(t) { }
};
template<class T> class FooDerived:public Foo<T>
{
};
int main()
{
FooDerived<int> d(5);
return 0;
}
template<class T> class Foo{
T tVar;
public:
Foo(T t) : tVar(t) { }
};
template<class T> class FooDerived:public Foo<T>
{
};
int main()
{
FooDerived<int> d(5);
return 0;
}
代码可以正确通过编译。
编译错误,FooDerived是一个继承模板类的非模板类,它的类型不能改变。
编译错误,tVal变量是一个不确定的类型。
编译错误,可以在FooDerived类中添加一个构造函数解决问题。
酱紫
template
class Foo {
T tVar;
public:
Foo(T t) : tVar(t) {}
};
template
class FooDerived : public Foo {
public:
FooDerived(T d) : Foo(d) {}
};
int main() {
FooDerived d(5);
return 0;
}