C++ 类模版
01类模版语法
在类上面加template
// 在类定义上方加上template <class T1,class T2>即可
template <class T1,class T2>
class Person{
public:
Person(T1 name,T2 age){
m_Name = name;
m_Age = age;
}
T1 m_Name;
T2 m_Age;
};
02 类模版与函数模版的区别
类模版与函数模版区别主要有两点:
- 类模版没有自动类型推导的使用方式
- 类模版在模版参数列表中可以有默认参数
//类模版与函数模版区别
template<class NameType ,class AgeType>
class Person{
public :
Person(NameType name,AgeType age){
this->m_Name = name;
this->m_Age = age;
}
void showPerson(){
cout << "name: "<< this->m_Name << " age = " << this->m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
- 类模版没有自动类型推导的使用方式
使用的时候必须进行显示指定类型,如
错误用法:Person p("孙悟空",1000);//错误,无法用自动类型推导;
正确用法:Person <string ,int >p("孙悟空",1000);//正确,只能用显式指定类型
//Person p("孙悟空",1000);//错误,无法用自动类型推导;
Person <string ,int >p("孙悟空",1000);//正确,只能用显式指定类型
p.showPerson();
- 类模版在模版参数列表中可以有默认参数
定义时:template<class NameType = string ,class AgeType = int >
使用时:Person <> p("猪八戒",999);
<>中为空即可
例如:
template<class NameType = string ,class AgeType = int >
class Person{
public :
Person(NameType name,AgeType age){
this->m_Name = name;
this->m_Age = age;
}
void showPerson(){
cout << "name: "<< this->m_Name << " age = " << this->m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
Person <> p("猪八戒",999);
p.showPerson();
03 类模版与成员函数创建时机
普通类中的成员函数一开始就可以创建
类模版中的成员函数在调用时才创建
使用见下面的例子:
//类模版中成员函数和普通类中成员函数创建时机是有区别的:
class Person1{
public:
void showPerson1(){
cout << "Person1 show" << endl;
}
};
class Person2{
public:
void showPerson2(){
cout << "Person2 show" << endl;
}
};
template<class T>
class MyClass
{
public:
T obj;
//类模版中的成员函数
void func1(){
obj.showPerson1();
}
void func2(){
obj.showPerson2();
}
};
void test01(){
MyClass<Person1> m;
m.func1();
//m.func2();
}
int main(){
test01();
}
04 类模版对象做函数是参数
类模版实例化出的对象,向函数传参的方式
一共有三种传入方式:
- 指定传入的类型 – 直接显示对象的数据类型(使用比较广泛)
void printPerson1(Person<string,int> &p){
p.showPerson();
}
Person<string ,int > p("孙悟空", 100);
printPerson1(p);
- 参数模版化 – 将对象中的参数变为模版进行传递
template<class T1,class T2>
void printPerson2(Person<T1,T2> &p){
p.showPerson();
cout << "T1 的类型为: " << typeid(T1).name() << endl;//NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE
cout << "T2 的类型为: " << typeid(T2).name() << endl;//苹果电脑int 显示i
}
Person<string ,int >p("猪八戒" ,90);
printPerson2(p);
- 整个类模版化 – 将这个对象类型 模版化进行传递
template<class T>
void printPerson3(T &p){
p.showPerson();
cout << "T 的类型为: " << typeid(T).name() << endl;//Person 类型
}
Person<string,int> p("唐僧",30);
printPerson3(p);
05 类模版与继承
当类模版碰到继承时 ,需要注意以下几点:
- 当子类继承的父类是一个类模版时,子类在声明的时候,要指定出父类中T的类型常用
- 如果不指定,编译器无法给子类分配内存
- 如果想灵活指定出父类中T的类型,子类也需要变为类模版
template<class T>
class Base{
T m;
};
//class Son : public Base // 错误,必须要知道父类中的T类型,才能继承给子类
class Son:public Base<int>
{
};
void test01(){
Son s1;
}
// 如果想灵活指定父类中T类型,子类也需要变类模版
template <class T1,class T2>
class Son2 :public Base<T2>{
public:
Son2(){
cout << "T1的类型为: " << typeid(T1).name() << endl;//i
cout << "T2的类型为: " << typeid(T2).name() << endl;//c
}
T1 obj;
};
Son2<int ,char> s2 ;
06 类模版成员函数类外实现
类模版中成员函数类外实现时,需要加上模版参数列表
例子如下:
template<class T1,class T2>
class Person{
public:
Person(T1 ,T2){
this->m_Name = name;
this->m_Age = age;
}
void showPerson();
T1 m_Name;
T2 m_Age;
};
//成员函数类外实现
template <class T1,class T2>
void Person<T1,T2>::showPerson(){
cout << "姓名: " << this->m_Name << " 年龄: " << this->m_Age << endl;
}
void test01(){
Person<string,int> p1("孙悟空",10);
p1.showPerson();
}
// 总结 :类模版中成员函数类外实现时,需要加上模版参数列表
07类模版分文件编写
在写类模版份文件编写时,会出现如下的一个问题:
类模版中成员函数创建时机是在调用阶段,导致分文件编写是链接不到
两种解决办法
- 第一种解决办法 : 直接包含源文件
- 第二种解决办法: 将.h 和 .cpp中的内容写到一起,将后缀名改为.hpp 这个后缀是常见写法,不是强制性的
第二种比较常用
08 类模块与友元
- 全局函数类内实现 - 直接在类内声明友元即可
举例:
template<class T1,class T2>
class Person{
//全局函数 类内实现
friend void printPerson(Person<T1,T2> &p){
cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
}
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>
class Person;
//类外实现
template <class T1,class T2>
void printPerson2(Person<T1,T2> &p){
cout << "类外实现 --- 姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
}
template<class T1,class T2>
class Person{
//全局函数 类外实现
//加空模版参数列表
//如果全局函数是类外实现,需要让编译器提前知道这个函数的存在
friend void printPerson2<>(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;
};
09 类模版案例-数组类封装
#pragma once
#include <iostream>
using namespace std;
template<class T>
class MyArray{
public:
//有参构造 参数 容量
MyArray(int capacity){
// cout << "MyArray 有参构造函数 调用" << endl;
this->m_Size = 0;
this->m_Capacity = capacity;
this->pAddress = new T[this->m_Capacity];
}
//拷贝构造
MyArray(const MyArray& arr){
// cout << "MyArray 拷贝构造函数 调用" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//编译器生成的默认拷贝构造函数会写的浅拷贝
//this->pAddress = arr.pAddress;
// 深拷贝
this->pAddress = new T[arr.m_Capacity];
//将arr中的数据都拷贝过来
for(int i = 0;i<this->m_Size;i++){
this->pAddress[i] = arr.pAddress[i];
}
}
//operator= 防止浅拷贝问题
MyArray & operator=(const MyArray & arr){
// cout << "MyArray operator= 防止浅拷贝问题 调用" << endl;
//先判断原来堆区是否有数据,如果有先释放
if(this->pAddress != NULL){
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 < arr.m_Size ; i++){
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
// 尾插法
void Push_Back(const T &val){
if(this->m_Size == this->m_Capacity){
return ;
}
this->pAddress[this->m_Size] = val;
this->m_Size++;
}
//通过下标方式访问数组中的元素
T& operator[](int index)
{
return this->pAddress[index];
}
//返回数组容量
int getCapacity(){
return this->m_Capacity;
}
// 返回数组大小
int getSize(){
return this->m_Size;
}
//尾删法
void Pop_Back(){
//让用户访问不到最后一个元素,即为尾删
if(this->m_Size == 0) {
return;
}
this->m_Size--;
}
// 析构函数
~MyArray(){
// cout << "MyArray 析构函数 调用" << endl;
if(this->pAddress != NULL){
delete this->pAddress;
this->pAddress = NULL;
}
}
private:
int m_Capacity = 0; //数组容量
int m_Size = 0 ; // 数组大小
T * pAddress = NULL; // 指针指向堆区开辟的真是数组
};