24.7.4学习目录
一.类模板
类模板实例化对象时,不能自动类型推导,需要指明其类型
template<class T1.class T2>
class Data{
private:
T1 a;
T2 b;
public:
Data(){}
Data(T1 a,T2 b){
this->a = a;
this->b = b;
}
void showData(){
cout<<a<<" "<<b;
}
};
void test1(){
//需要人为的指定类型
Data<int,int> ob(10,20);
ob.showData();
}
1.1 类模板的成员函数
在类外实现的格式
template<class T1.class T2> //类模板的作用域只在该类中,无法作用到类外
class Data{
private:
T1 a;
T2 b;
public:
Data(){}
Data(T1 a,T2 b);
void showData(){
cout<<a<<" "<<b;
}
};
template<class T1,class T2> //这里添加是因为其作用域的原因
Data<T1,T2>::Data(T1 a,T2 b){ //类的名称变为了Data<T1,T2>,若写成Data编译会不通过
this->a = a;
this->b = b;
}
1.2 函数模板作为类模板的友元
template<class T1.class T2>
class Data{
template<class T3,class T4>
friend void myPrintData(Data<T3,T4> &ob);
private:
T1 a;
T2 b;
public:
Data(){}
Data(T1 a,T2 b){
this->a = a;
this->b = b;
}
void showData(){
cout<<a<<" "<<b;
}
};
template<class T3,class T4> void myPrintData(Data<T3,T4> &ob){
cout<<ob.a<<" "<<ob.b;
}
void test1(){
//需要人为的指定类型
Data<int,int> ob(10,20);
myPrintData(ob);
}
1.3 普通函数作为类模板的友元
需要指明泛型的类型即可,其他都和函数模板作为友元一样
1.4 类模型与函数分离实现
//
#include<iostream>
#include "data.h" //类声明的文件
#include "data.cpp" //其中类中成员函数的实现,包含上这个文件是为了让类模板中成员函数在第二次编译时,能找到其实现的位置
using namespace std;
int main(int argc.char *argv[]){
Data<int,int> ob1;//如果没有包含上函数实现的头文件,编译时会报错
ob1.showData();
return 0;
}
还有一种方法,其是将成员函数实现放在头文件中,并将头文件的尾名改为.hpp(其实可以不用改)
1.5 普通类继承类模板
类模板被继承时需要指明泛型的类型
template<class T1.class T2>
class Data{
template<class T3,class T4>
friend void myPrintData(Data<T3,T4> &ob);
private:
T1 a;
T2 b;
public:
Data(){}
Data(T1 a,T2 b){
this->a = a;
this->b = b;
}
void showData(){
cout<<a<<" "<<b;
}
};
class Son1:public Data<int,char>
{
public:
int c;
public:
Son1(int a,char b,int c):Data<int,char>(a,b){
this->c = c;
}
};
1.6 类模板继承类模板
template<class T1.class T2>
class Data{
template<class T3,class T4>
friend void myPrintData(Data<T3,T4> &ob);
private:
T1 a;
T2 b;
public:
Data(){}
Data(T1 a,T2 b){
this->a = a;
this->b = b;
}
void showData(){
cout<<a<<" "<<b;
}
};
template<class T1.class T2,class T3>
class Son1:public Data<T1,T2>
{
public:
T3 c;
public:
Son1(T1 a,T2 b,T3 c):Data<T1,T2>(a,b){
this->c = c;
}
};
二.类型转换
c++提供了一套新的类型转换,其允许不同种类的强制转换,并能更好的识别。
上行和下行转换的概述:
上行:子类空间给父类指针保存;安全
下行:父类空间给子类指针保存;不安全
1.static_cast静态类型转换
用于父类和子类之间指针或引用的转换
//构建三个类
class Base{};
class Son:public Base{};
class Other{};
基本类型的转换:
int num = static_cast<int>(3.14);
上行的转换:
Base *p = static_cast<Base *>(new Son);
下行的转换:不安全
Son *p = static_cast<Son *>(new Base);
不相关类型转换:不支持的
//Base *p = static_cast<Base *>(new Other);//是错误的
2.dynamic_cast动态类型转换
主要用于上行转换
Base *p = dynamic_cast<Base *>(new Son);
其他类型的转换是不支持的
3.const_cast常量转换
将const修饰的指针或引用转换为非const
const int *p1;
int *p2 = const_cast<int *>(p1);
const int &ob = 10;
int &ob1 = const_cast<int &>(ob);
将非const的指针或引用转换为const
int *p1;
const int *p2 = const_cast<int *>(p1);
int &ob = 10;
const int &ob1 = const_cast<int &>(ob);
4.reinterpret_cast重新解释转换(最不安全)
基本类型的转换:不支持的
//int num = reinterpret_cast<int>(3.14);
基本类型指针:
float *q;
int *p = reinterpret_cast<int *>(q);
上行的转换:
Base *p = reinterpret_cast<Base *>(new Son);
下行的转换:
Son *p = reinterpret_cast<Son *>(new Base);
不相关类型转换:
Base *p = reinterpret_cast<Base *>(new Other);