C++Day8——函数模板与类模板
函数模板
#include <iostream>
using namespace std;
void mySwapInt(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
void mySwapDouble(double& a, double& b)
{
double temp = a;
a = b;
b = temp;
}
//利用模板实现通用交换函数
//T代表一个通用数据类型,告诉编译器如果下面紧跟着的函数或类中出现T不要报错
template<typename T>
void mySwap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
//模板不能单独使用,必须指定出T才可以使用
void test01()
{
int a = 10;
int b = 20;
//mySwapInt(a, b);
char c1 = 'c';
//1、自动类型推导,必须推导出一致的T数据类型才可以正常的使用模板
mySwap(a, b);
//mySwap(a,c1);错误 推导不出一致的T无法调用
cout << a << endl;
cout << b << endl;
double c = 1.1;
double d = 2.2;
//mySwapDouble(c,d);
//2、显示指定类型
mySwap<double>(c, d);
//mySwap<double>(c1,d);类型不一致
cout << c << endl;
cout << d << endl;
}
int main()
{
test01();
return EXIT_SUCCESS;
}
函数模板和普通函数的区别以及调用规则
#include <iostream>
using namespace std;
template<class T>
T myAdd(T a, T b)
{
return a + b;
}
int myAdd2(int a, int b)
{
return a + b;
}
void test01()
{
int a = 10;
int b = 20;
char c = 'c';
//myAdd(a,c);使用自动类型推导无法发生隐式类型转换
cout << myAdd<int>(a, c) << endl;
cout << myAdd2(a, c) << endl;//普通函数可以发生隐式类型转换
}
//函数模板和普通函数调用规则
template<class T>
void myPrint(T a, T b)
{
cout << "函数模板调用" << endl;
}
template<class T>
void myPrint(T a,T b,T c)
{
cout << "函数模板(T a,T b,T c)调用" << endl;
}
void myPrint(int a, int b)
{
cout << "普通函数调用" << endl;
}
void test02()
{
//如果函数模板和普通函数都可以调用,优先调用普通函数
int a = 10;
int b = 20;
myPrint(a, b);
//如果想强制调用函数模板,可以使用空模板参数列表
myPrint<>(a, b);
//函数模板也可以发生重载
myPrint(a, b, 10);
//如果函数模板可以产生更好的匹配,优先使用函数模板
char c = 'c';
char d = 'd';
myPrint(c, d);//调用函数模板
}
int main()
{
test01();
test02();
return EXIT_SUCCESS;
}
模板机制和模板局限性
-
函数模板机制
- 编译器并不是把函数模板处理成能够处理任何类型的函数
- 函数模板通过具体类型产生不同的函数
- 编译器会对函数模板进行二次百衲衣,在声明的地方对模板代码本身进行编译,在调用的地方对参数替换后的代码进行编译
-
模板的局限性
-
#include<iostream> using namespace std; #include <string> class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } string m_Name; int m_Age; }; //显示两个变量 对比 函数 template<class T> bool myCompare(T &a ,T&b) { if (a == b) { return true; } return false; } //利用具体化技术 实现对自定义数据类型 提供特殊模板 template<> bool myCompare(Person &a, Person &b) { if (a.m_Name == b.m_Name && a.m_Age == b.m_Age) { return true; } return false; } void test01() { //int a = 10; //int b = 10; //bool ret = myCompare(a, b); //if (ret ) //{ // cout << "a == b" << endl; //} //else //{ // cout << "a != b" << endl; //} Person p1("Tom", 19); Person p2("Tom", 20); bool ret = myCompare(p1, p2); if (ret) { cout << "p1 == p2" << endl; } else { cout << "p1 != p2" << endl; } } int main(){ test01(); return EXIT_SUCCESS; }
-
类模板基本语法
#include <iostream>
#include <string>
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 << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl;
}
NAMETYPE m_Name;
AGETYPE m_Age;
};
void test01()
{
//自动类型推导 类模板不可以使用自动类型推导
//Person p1("孙悟空", 100);
//显示指定类型
Person<string> p1("孙悟空", 10);
p1.showPerson();
}
int main()
{
test01();
return EXIT_SUCCESS;
}
类模板做函数参数
#include <iostream>
#include <string>
using namespace std;
template<class T1, class T2>
class Person
{
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl;
}
T1 m_Name;
T2 m_Age;
};
//指定传入类型
void doWork(Person<string, int>&p)
{
p.showPerson();
}
void test01()
{
Person<string, int>p("孙悟空", 999);
doWork(p);
}
//参数模板化
template<class T1,class T2>
void doWork2(Person<T1, T2>& p)
{
p.showPerson();
}
void test02()
{
Person<string, int>p("猪八戒", 998);
doWork2(p);
}
//整个类模板化
template<class T>
void doWork3(T &p)
{
p.showPerson();
}
void test03()
{
Person<string, int>p("唐僧", 10000);
doWork3(p);
}
int main()
{
test01();
test02();
test03();
return EXIT_SUCCESS;
}
类模板碰到继承的问题以及解决
#include <iostream>
#include <string>
using namespace std;
template<class T>
class Base
{
public:
T m_A;
};
//必须指定出父类中T的数据类型,才能给子类分配内存
class Son : public Base<int>
{
};
template<class T>
class Base2
{
public:
T m_A;
};
template<class T1,class T2>
class Son2 : public Base<T2>
{
public:
Son2()
{
cout << typeid(T1).name() << endl;
cout << typeid(T2).name() << endl;
}
T1 m_B;
};
void test01()
{
Son2<int, double>s;
}
int main()
{
test01();
return EXIT_SUCCESS;
}
类模板中的成员函数类外实现
#include <iostream>
#include <string>
using namespace std;
template<class T1,class T2>
class Person
{
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl;
}
T1 m_Name;
T2 m_Age;
};
void test01()
{
Person<string, int>p("Tom", 10);
p.showPerson();
}
int main()
{
test01();
return EXIT_SUCCESS;
}
类模板的分文件编写问题以及解决
- 类模板中的成员函数,不会一开始创建,因此导致分文件编写时连接不到函数的实现,出现无法解析的外部命令错误
- 解决方式
- 直接包含.cpp文件(不推荐)
- 将类声明和实现写到同一个文件中,将文件的后缀名改为.hpp即可
类模板碰到友元问题以及解决
#include<iostream>
using namespace std;
#include <string>
template<class T1,class T2>
class Person;
template<class T1,class T2>
void printPerson2(Person<T1, T2> &p);
template<class T1,class T2>
void printPerson3(Person<T1, T2>&p)
{
cout << "类外实现 --- 姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
}
template<class T1, class T2>
class Person
{
//1、友元函数 类内实现
friend void printPerson(Person<T1, T2> &p)
{
cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
}
//2、友元函数 类外实现
friend void printPerson2<>(Person<T1, T2> &p);
friend void printPerson3<>(Person<T1, T2> &p);
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
private:
T1 m_Name;
T2 m_Age;
};
template<class T1,class T2>
void printPerson2(Person<T1, T2> &p)
{
cout << "类外实现 --- 姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
}
void test01()
{
Person <string, int> p("Tom", 100);
printPerson(p);
printPerson2(p);
printPerson3(p);
}
int main(){
test01();
return EXIT_SUCCESS;
}
类模板应用——数组类封装
#include<iostream>
using namespace std;
#include "myArray.hpp"
#include <string>
class Person
{
public:
Person(){};
Person(string name,int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void myPrintInt(MyArray <int> &myIntArr)
{
for (int i = 0; i < myIntArr.getSize();i++)
{
cout << myIntArr[i] << endl;
}
}
void test01()
{
MyArray <int> myIntArr(100);
for (int i = 0; i < 10;i++)
{
myIntArr.pushBack(i + 100);
}
myPrintInt(myIntArr);
}
void myPrintPerson(MyArray<Person> &myPersonArr)
{
for (int i = 0; i < myPersonArr.getSize();i++)
{
cout << "姓名: " << myPersonArr[i].m_Name << " 年龄:" << myPersonArr[i].m_Age << endl;
}
}
void test02()
{
MyArray<Person> myPersonArr(100);
Person p1("孙悟空", 1000);
Person p2("猪八戒", 800);
Person p3("唐僧", 500);
Person p4("沙悟净", 300);
Person p5("白龙马", 10000);
myPersonArr.pushBack(p1);
myPersonArr.pushBack(p2);
myPersonArr.pushBack(p3);
myPersonArr.pushBack(p4);
myPersonArr.pushBack(p5);
myPrintPerson(myPersonArr);
cout << "数组容量: " << myPersonArr.getCapacity() << endl;
cout << "数组大小: " << myPersonArr.getSize() << endl;
}
int main(){
// test01();
test02();
return EXIT_SUCCESS;
}