C++之泛型之类模板
作用:
建立一个通用类,类中的成员数据类型可以不具体制定,用一个虚拟的类型来代表
template类
类模板和函数模板语法相似,在声明的模板template后面加类,此类称为类模板。
example1:
#include#include using namespace std; template class Person { public: Person(nameType name,AgeType age) { this->m_name = name; this->m_age = age; } void show() { cout << "name: " << this->m_name << endl; cout << "age: " << this->m_age << endl; } nameType m_name; AgeType m_age; }; void test() { Person p("Tom", 20); p.show(); } int main() { test(); system("pause"); return 0; }
2.类模板与函数模板区别
2.1 类模板没有自动类型推导的使用方式即只有显式指定
2.2 类模板可以在模板参数列表中可以有默认参数
以上文的Person类为例
templateclass Person { public: Person(nameType name,AgeType age) { this->m_name = name; this->m_age = age; } void show() { cout << "name: " << this->m_name << endl; cout << "age: " << this->m_age << endl; } nameType m_name; AgeType m_age; };
template
可以将age的类型直接指定为int型,其它不变也可以正常运行。
3.类模板中成员函数的创建
普通类中的成员函数一开始就创建
类模板中的成员函数在调用时才创建
4.类模板对象做函数参数
传入方式一共有三种
1.指定传入的类型-----------直接显示随想的数据类型
2.参数模板化------------------------将对象中的参数变为模板进行传递
3.整个类模板化--------将这个对象类型模板化进行传递
example2
#include#include using namespace std; template class Person { public: Person(nameType name,AgeType age) { this->m_name = name; this->m_age = age; } void show() { cout << "name: " << this->m_name << endl; cout << "age: " << this->m_age << endl; } nameType m_name; AgeType m_age; }; //传递参数 void showPerson(Person &p) { p.show(); } void test() { Person p("Tom", 20); showPerson(p); } int main() { test(); system("pause"); return 0; }
方式一:
void showPerson(Person&p) { p.show(); }
类似于显示指定
example3
在example2的基础上添加
templatevoid showPerson1(Person &p) { p.show(); }
void test1()
{
Person p("Tom", 20);
showPerson(p);
}
方式二利用了编译器的自动推断参数的数据类型
example4:
方式三:将整个类实现模板化,在example2基础上添加
templatevoid showPerson2(T &p) { p.show(); }
void test2()
{
Person p("Tom", 20);
showPerson2(p);
}
方式三将类实现了模板化,也使用了自动类型推导,推导出来的类型默认为类名,该包含了类中全部的参数数据类型
全部程序展示:
#include#include using namespace std; template class Person { public: Person(nameType name,AgeType age) { this->m_name = name; this->m_age = age; } void show() { cout << "name: " << this->m_name << endl; cout << "age: " << this->m_age << endl; } nameType m_name; AgeType m_age; }; //传递参数 void showPerson(Person &p) { p.show(); } template void showPerson1(Person &p) { p.show(); } template void showPerson2(T &p) { p.show(); } void test() { Person p("Tom", 20); showPerson(p); } void test1() { Person p("Tom", 20); showPerson1(p); } void test2() { Person p("Tom", 20); showPerson2(p); } int main() { test(); test1(); test2(); system("pause"); return 0; }
上述结果展示
5.继承问题
1.当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类的类型
class Son1:public Person{ public: };
如果不指定 的话,编译器无法为子类分配内存
2.如果灵活指出父类的类型,需要将子类也变为类模板
example
:
templateclass Son2 :public Person { public: Son2() { cout <<"T1的数据类型为:"<< typeid(T1).name() << endl; cout <<"T2的数据类型为:"<< typeid(T2).name() << endl; } };
全部程序:
#include#include using namespace std; template class Person { public: T m; }; class Son1:public Person { public: }; template class Son2 :public Person { public: Son2() { cout <<"T1的数据类型为:"<< typeid(T1).name() << endl; cout <<"T2的数据类型为:"<< typeid(T2).name() << endl; } }; void test() { Son2 s2; } int main() { test(); system("pause"); return 0; }



