目录
一、普通类的成员函数模板
不论模板类还是不同类,它们的成员函数都可以是一个函数模板,统称为成员函数模板。除了虚函数,虚函数不可以成为模板函数,因为虚函数需要在子类中重新定义,但是因为没有具体的参数类型,无法生成其对应的代码,所以虚函数并不能同时为模板函数。
类内函数体:
class Goodname
{
public:
template<class T>
void CoutInformation(T a)
{
cout<<a<<endl;
}
};
Goodname a;
a.CoutInformation(3);//函数模板可以自行推断
类外函数体:
class A
{
public:
template<class T>
void Func(T a);
};
template<class T>
void A::Func(T a)
{
cout << a << endl;
}
二、类模板的成员函数模板
类内函数体:
template<class T>
class A
{
public:
template<