首页 > 技术文章 > 类模板派生出新的类模板

YXBLOGXYY 2021-01-05 20:47 原文

#include<iostream>
using namespace std;
template<class T>
class Animal{
public:
Animal(){
cout << mAge << "岁的动物在叫" << endl;
}
public:
T mAge;
};
class Cat :public Animal<int>{  // 注意这里如果不加<int>的话就会报错,因为类在生成对象的时候,编译器要为其分配空间,需要知道具体的类型才能分配空间


};
template<class T>
class SmallAnimal : public Animal<T>{


};
int main(){

 

return 0;
}

推荐阅读