C++另一种编程思想被称为泛型编程,主要利用的技术就是模板
C++提供两种模板机制:函数模板和类模板
模板的特点:
函数模板作用:
建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来制定
语法:template
#includeusing namespace std; //函数模板 template//告诉编译器,后面的代码中紧跟着的T不要报错,T是一个通用数据类型 void mySwap(T& a, T& b) { T temp = a; a = b; b = temp; } int main() { int a = 10; int b = 20; //两种方法使用函数模板 //自动类型推导 mySwap(a, b); cout << "a=" << a << "\tb=" << b << endl; //显式指定类型 mySwap(a, b); cout << "a=" << a << "\tb=" << b << endl; return 0; }
//模板必须要确定出T的数据类型,才可以使用 template//注释掉可正常运行 void func(T& a, T& b) { cout << "hello" << endl; } int main() { func();//没有与参数列表匹配的函数模板实例 return 0; }
templateT func(T a, T b) { return a + b; } int main() { int a = 10; char b = 10; //如果利用显示指定类型的方式,可以发生隐式类型转换 cout << func(a, b) << endl;//可正常运行 //如果利用自动类型推导,不会发生隐式类型转换 cout << func(a, b) << endl;//会报错 return 0; }
void func(int a, int b) { cout << "普通函数" << endl; } templatevoid func(T a, T b) { cout << "模板" << endl; } int main() { //如果函数模板和普通函数都可以实现,优先调用普通函数 func(1, 1); //通过空模板参数列表来强制调用函数模板 func<>(1, 1); return 0; }
templatevoid func(T a, T b) { cout << "模板" << endl; } templatevoid func(T a, T b,T c) { cout << "重载的模板" << endl; } int main() { func<>(1, 1); //函数模板也可以发生重载 func<>(1, 1, 1); return 0; }
模板不是万能的,有些特定数据类型,需要用具体化方式做特殊实现
class Person { public: Person(string name, int age) { m_name = name; m_age = age; } string m_name; int m_age; }; templatebool func(T& a, T& b) { if (a == b) return true; else return false; } //利用具体化Person的版本实现代码,具体化优先调用 template<> bool func(Person& a, Person& b) { if (a.m_name == b.m_name && a.m_age == b.m_age) return true; else return false; }
类模板作用:
语法:template
类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板
templateclass Person { public: Person(NameType name, AgeType age) { this->m_name = name; this->m_age = age; } void show() { cout << m_name << m_age << endl; } NameType m_name; AgeType m_age; }; int main() { Persona("张三", 10); a.show(); return 0; }
类模板与函数模板区别主要有两点:
//类模板中可以有默认参数 templateclass Person { public: Person(NameType name, AgeType age) { this->m_name = name; this->m_age = age; } void show() { cout << m_name << m_age << endl; } NameType m_name; AgeType m_age; }; int main() { //类模板没有自动类型推导的使用方式 //Person a("张三", 10); Persona("张三", 10);//只能用显示指定类型 a.show(); return 0; }
class A { public: void show() { cout << "A" << endl; } }; templateclass B { public: T t; void show() { t.show(); } }; int main() { B b; b.show(); return 0; }
类模板实例化出的对象,有三种向函数传参的方式:
templateclass A { public: A(T t) { this->m_t = t; } T m_t; void show() { cout << m_t << endl; } }; //指定传入类型 void show(A& a) { a.show(); } int main() { Aa(10); show(a); return 0; }
templateclass A { public: A(T t) { this->m_t = t; } T m_t; void show() { cout << m_t << endl; } }; //参数模板化 templatevoid show(A& a) { a.show(); } int main() { Aa(10); show(a); return 0; }
templateclass A { public: A(T t) { this->m_t = t; } T m_t; void show() { cout << m_t << endl; } }; //参数模板化 templatevoid show(T a) { a.show(); cout << "T的类型为" << typeid(T).name() << endl; } int main() { Aa(10); show(a); return 0; }
templateclass A { }; //必须知道父类中T的类型,才能继承给子类 class B :public A{ };
class A { }; //如果想灵活指定父类中T的类型,子类也需要变为类模板 templateclass B :public A{ };
templateclass Person { public: Person(T1 name, T2 age); T1 m_name; T2 m_age; }; //构造函数类外实现 templatePerson::Person(T1 name, T2 age) { this->m_age = age; this->m_name = name; }
templateclass Person { public: void show(); T1 m_name; T2 m_age; }; //成员函数类外实现 templatevoid Person::show() { cout << m_name << "\t" << m_age << endl; }
问题:
两种解决方式:
.cpp
源文件.hpp
,.hpp
是约定的名称,不是强制规定全局函数类内实现:直接在类内声明友元即可
全局函数类外实现:需要提前让编译器知道全局函数的存在
建议全局函数做类内实现,用法简单,而且编译器可以直接识别
templateclass Test { friend void func(Testtest) { cout << "全局函数类内实现 " << test.m_name << endl; } T1 m_name; public: Test(T1 name) { this->m_name = name; } }; int main() { Testa("张三"); func(a); return 0; }
templateclass Test; templatevoid func(Testtest); //用到的东西要提前让编译器知道 templateclass Test { //加空模板参数列表,调用函数模板 friend void func<>(Testtest); T1 m_name; public: Test(T1 name) { this->m_name = name; } }; templatevoid func(Testtest) { cout << "全局函数类外实现 " << test.m_name << endl; } int main() { Testa("张三"); func(a); return 0; }
直接包含.hpp
即可使用
#pragma once //自己的通用数组类 #include#includeusing namespace std; templateclass MyArray { public: MyArray(int capacity) { cout << "有参构造" << endl; this->m_Capacity = capacity; this->m_Size = 0; this->pAddress = new T[this->m_Capacity]; } MyArray(const MyArray& arr) { cout << "拷贝构造" << endl; this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; //深拷贝 this->pAddress = new T[arr.m_Capacity]; //将arr中的数据都拷贝过来 for (int i = 0; i < this->m_Size; i++) { this->pAddress[i] = arr.pAddress[i]; } } ~MyArray() { if (this->pAddress != NULL) { cout << "析构" << endl; delete[] this->pAddress; this->pAddress = NULL; } } MyArray& operator=(const MyArray& arr) { //先判断原来堆区是否有数据,如果有,先释放 if (pAddress != NULL) { cout << "operator=调用" << endl; delete[] this->pAddress; this->pAddress = NULL; this->m_Capacity = 0; this->m_Size = 0; } //深拷贝 this->m_Capacity = arr.m_Capacity; this->m_Size = arr.m_Size; this->pAddress = new T[arr.m_Capacity]; for (int i = 0; i < this->m_Size; i++) { this->pAddress[i] = arr.pAddress[i]; } return *this; } //尾插法 void Push_Back(const T& val) { if (this->m_Capacity == this->m_Size) { return; } this->pAddress[this->m_Size] = val; this->m_Size++; } //尾删法 void Pop_Back() { //让用户访问不到最后一个元素,即是尾删 if (m_Size == 0) { return; } this->m_Size--; } //通过下标访问数组中的元素 T& operator[](int index) { return this->pAddress[index];//不考虑边界,由用户自行处理 } //返回数组容量 int getCapacity() { return this->m_Capacity; } //返回数组大小 int getSize() { return this->m_Size; } private: T* pAddress;//指针指向堆区开辟的真实数组 int m_Capacity; int m_Size; };
Copyright © 2023 leiyu.cn. All Rights Reserved. 磊宇云计算 版权所有 许可证编号:B1-20233142/B2-20230630 山东磊宇云计算有限公司 鲁ICP备2020045424号
磊宇云计算致力于以最 “绿色节能” 的方式,让每一位上云的客户成为全球绿色节能和降低碳排放的贡献者