在模板类的继承中,需要注意以下两点:
- 必须指明父类的模板类型,因为要分配内存空间
- 如果子类是模板类,要么用具体类型指定父类的模板类型,要么用子类的泛型来指定父类。
template <typename T>
class Parent{
public:
Parent(T p)
{
this->p = p;
}
private:
T p;
};
//如果子类不是模板类,需要指明父类的具体类型
class ChildOne: public Parent<int> {
public:
ChildOne(int a,int b):Parent(b)
{
this->cone = a;
}
private:
int cone;
};
//如果子类是模板类,可以用子类的泛型来表示父类
template <typename T>
class ChildTwo: public Parent<T> {
public:
ChildTwo(T a, T b):Parent<T>(b)
{
this->ctwo = a;
}
private:
T ctwo;
};
怎么调用模板父类的方法
https://blog.csdn.net/ooooo12345re/article/details/51218449