函数模版
引出
比如我们要用一个交换数据的函数:
void swapFunc(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
但是当我们要传入double类型,float类型,long类型,则需要再定义三个这样的函数
这些函数逻辑相同,只是参数类型不同。这时我们可以考虑使用模板类来实现。
实现
#include <iostream>
using namespace std;
template <class T> // 告诉编译器,下面出现的T是一个通用类型,class也可以为typename关键字
//会根据传入的参数类型进行自动类型推导
void swapFunc(T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10;
int b = 20;
//1、会进行自动类型推导
swapFunc(a, b);
cout << "a = " << a << endl; //20
cout << "b = " << b << endl; //10
double c = 1;
double d = 2;
swapFunc(c, d);
cout << "c = " << c << endl; //2
cout << "d = " << d << endl; //1
int e = 3;
char f = 'f';
//swapFunc(e,d); 推导不出来报错
//2、显示指定类型
int g = 4;
int h = 5;
swapFunc<int>(g, h);
cout << g << endl; //5
cout << h << endl; //4
}
如果普通函数和模板函数重载,则优先使用普通函数,普通函数不匹配再找模版函数。
但是如果模版函数比普通函数更匹配,比如普通函数需要进行隐士转换时,则先调用模版函数。
模版实现机制
编译器对代码进行二次编译,第一次是对代码进行编译,第二次是对替换类型T后对代码进行编译。
类模版的使用
#include <iostream>
using namespace std;
template <class NameType, class AgeType> //指定下方的类模版
class Person
{
public:
Person(NameType name, AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << m_Name << " " << m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
int main()
{
//类模版不支持自动类型推导
//Person p("悟空",100);
//只能显示指定类型
Person<string, int> p("悟空", 100);
p.showPerson();
}
成员函数的创建时机
#include <iostream>
using namespace std;
template <class T> //指定下方的类模版
class test
{
public:
T obj;
void func1()
{
obj.showPerson1();
}
void func2()
{
obj.showPerson2();
}
};
class Person1
{
public:
void showPerson1()
{
cout << "Person1" << endl;
}
};
class Person2
{
public:
void showPerson2()
{
cout << "Person2" << endl;
}
};
int main()
{
test<Person1> t; //创建对象时指定T的类型
t.func1();
//t.func2(); 报错
}
上面例子可以看到,Person1类中可以调用showPerson1 但是调用showPerson2 时会报错,也就是说成员函数只有等到运行时才会创建该函数。而不是编译时。
类模版做函数参数
#include <iostream>
using namespace std;
template <class NameType, class AgeType = int> //类模版可以有默认类型
class Person
{
public:
Person(NameType name, AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << m_Name << " " << m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
//1、指定参数类型传参
void doWork1(Person<string, int> &p)
{
p.showPerson();
}
//2、参数模版化,可以不指定传入的类型,在调用的时候再指定
template <class NameType, class AgeType>
void doWork2(Person<NameType, AgeType> &p)
{
p.showPerson();
}
//3、整体参数化
template <class T>
void doWork3(T &p)
{
p.showPerson();
}
int main()
{
Person<string, int> p1("MT", 10);
doWork1(p1);
Person<string, int> p2("KG", 20);
doWork2(p2);
Person<string, int> p3("DP", 30);
doWork2(p3);
}
子类继承模版类
#include <iostream>
using namespace std;
template <class T>
class Base
{
public:
T m_A;
};
//子类继承父类必须指明父类中的T的类型,不然报错
class Child1 : public Base<int>
{
};
//子类也是一个模版类
template <class T1, class T2>
class Child2 : public Base<T2>
{
T1 m_B;
};
int main()
{
Child2<int, double> child; //用户指定类型
}
类外实现成员函数
#include <iostream>
using namespace std;
template <class NameType, class AgeType = int> //类模版可以有默认类型
class Person
{
public:
Person(NameType name, AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson();
NameType m_Name;
AgeType m_Age;
};
//类外实现成员函数:要使用函数模版
template <class NameType, class AgeType>
void Person<NameType, AgeType>::showPerson()
{
cout << m_Name << " " << m_Age << endl;
}
int main()
{
Person<string,int> p("s",10);
p.showPerson();
}
类模版的分文件编写
不要类模版中成员函数的声明和实现如果写到一个.h文件一个.cpp文件时,其他文件中调用的时候会找不到函数。
解决方法:
把类模版的成员函数声明和实现都写在同一个文件中,后缀名为.hpp文件,所以一看到.hpp文件就说明是类模版。