8,四个类型转换const_cast、reinterpret_cast、dynamic_cast、static_cast

news/2024/7/7 22:29:31

类型转换const_cast、reinterpret_cast、dynamic_cast、static_cast

  • const_cast
  • reinterpret_cast
  • dynamic_cast
  • static_cast

const_cast

被const修饰的函数可以被访问,但是不能被修改成员变量
const_cast可以去掉const

#include <iostream>
using namespace std;

class FHello
{
public:
	FHello();
	void Init();/*const*/
private:
	int a;
	int b;
	float c;
};
FHello::FHello()
{
	a = 0;
	b = 10;
	c = 20.f;
}
void FHello::Init()/*const*/
{
	auto Hello_a = [&]()
	{
		cout << a << endl;
	};
	auto Hello_b = [&]()->bool
	{
		cout << b << endl;
		return true;
	};

	Hello_a();
	if (bool bHello = Hello_b())
	{
		cout << "true" << endl;
	}
	else
	{
		cout << "false" << endl;
	}
}
int main()
{
	const FHello* HelloTest = new FHello();
	//HelloTest->Init();因为HelloTest被const修饰了而上面代码中没有用const修饰

	//用const_cast去掉const
	FHello* Test = const_cast<FHello*>(HelloTest);
	Test->Init();
	//编译出错会返回NULL

	//c风格的强转,是万能强转//容易出问题,万一转换失败了变成野指针了也不知道,少用
	FHello* NewTest = (FHello*)HelloTest;
	NewTest->Init();

	delete HelloTest;
	return 0;
}

reinterpret_cast

把指针转换成int,也可以把int转成指针,也可以指针转换成指针

#include <iostream>
using namespace std;

class FHello
{
public:
	FHello();
	void Init();/*const*/
private:
	int a;
	int b;
	float c;
};
FHello::FHello()
{
	a = 0;
	b = 10;
	c = 20.f;
}
void FHello::Init()/*const*/
{
	auto Hello_a = [&]()
	{
		cout << a << endl;
	};
	auto Hello_b = [&]()->bool
	{
		cout << b << endl;
		return true;
	};

	Hello_a();
	if (bool bHello = Hello_b())
	{
		cout << "true" << endl;
	}
	else
	{
		cout << "false" << endl;
	}
}
int main()
{
	//const_cast
	const FHello* HelloTest = new FHello();
	FHello* Test = const_cast<FHello*>(HelloTest);
	Test->Init();
	FHello* NewTest = (FHello*)HelloTest;
	NewTest->Init();


	//reinterpret_cast
	//reinterpret_cast,先转换成int,再转换成指针
	int a = reinterpret_cast<int>(NewTest);
	FHello*b = reinterpret_cast<FHello*>(a);
	b->Init();

	//也可以转换成int*指针,也可以转换回来
	int* a2 = reinterpret_cast<int*>(NewTest);
	FHello* b2 = reinterpret_cast<FHello*>(a2);
	b2->Init();

	//可以转换成float*指针,但是不可以用float类型
	float* a3 = reinterpret_cast<float*>(NewTest);
	FHello* b3 = reinterpret_cast<FHello*>(a3);
	b3->Init();

	delete HelloTest;
	return 0;
}

dynamic_cast

动态的转化:可以向上转化也可以向下转化
向上转化和向下转化的意思是在数层的继承关系中,可以向父类或派生类进行转化
向下转换时会进行类型检测,如果类型无法转换回返回NULL。是一个比较安全的转换
接口(父类)必须有虚函数,否则会报错

#include <iostream>
using namespace std;

class FHello
{
public:
	FHello();
	virtual ~FHello();
	void Init();/*const*/
private:
	int a;
	int b;
	float c;
};
FHello::FHello()
{
	a = 0;
	b = 10;
	c = 20.f;
}
FHello::~FHello() {}
void FHello::Init()/*const*/
{
	auto Hello_a = [&]()
	{
		cout << a << endl;
	};
	auto Hello_b = [&]()->bool
	{
		cout << b << endl;
		return true;
	};

	Hello_a();
	if (bool bHello = Hello_b())
	{
		cout << "true" << endl;
	}
	else
	{
		cout << "false" << endl;
	}
}

class FHello1:public FHello
{
public:
	void Hello1temp() {}
private:
	int a;
};

class FHello2 :public FHello1
{

};

int main()
{
	//const_cast
	const FHello* HelloTest = new FHello();
	FHello* Test = const_cast<FHello*>(HelloTest);
	Test->Init();
    FHello* NewTest = (FHello*)HelloTest;
	NewTest->Init();


	//reinterpret_cast
	int a = reinterpret_cast<int>(NewTest);
	FHello*b = reinterpret_cast<FHello*>(a);
	b->Init();
	int* a2 = reinterpret_cast<int*>(NewTest);
	FHello* b2 = reinterpret_cast<FHello*>(a2);
	b2->Init();
	float* a3 = reinterpret_cast<float*>(NewTest);
	FHello* b3 = reinterpret_cast<FHello*>(a3);
	b3->Init();


	//dynamic_cast
	FHello* h1 = new FHello1();//这一步也是向上转换

	//向下转换:
	FHello1* A = dynamic_cast<FHello1*>(h1);
	A->Hello1temp();

	//向上转换,用的少,因为有更简单的方法,如上
	FHello* B = dynamic_cast<FHello*>(A);
	B->Init();

	FHello* B2 = A;//进行隐式转换

	FHello2* FHA2 = dynamic_cast<FHello2*>(h1);//转换失败,输出NULL
	if (FHA2)
	{
	}

	delete HelloTest;
	delete h1;
	return 0;
}

static_cast

可以向上转化也可以向下转化
向下转换时不安全
static_cast不可以转换const、volitale、_unaligned

static_cast和dynamic_cast相比static_cast可以没有虚函数,而dynamic_cast必须有虚函数

格式

FHello* A=static_cast<FHello*>(B);

http://lihuaxi.xjx100.cn/news/1445901.html

相关文章

一文秒懂HTTP协议到底是什么?原理?

目录 1.什么是http协议&#xff1f; 2.http协议的版本&#xff1f; 3.http文本框架 4.http请求报文 5.http报文格式 6.http响应报文 7.HTTP的状态码 8.HTTP首部介绍 9.什么是URL和URI&#xff1f; 10.CGI是什么&#xff1f; 1.什么是http协议&#xff1f; http&#…

ATF BL1 UFS初始化简单分析

ATF BL1 UFS初始化分析 1 ATF的下载链接2 ATF BL1 UFS 初始化简易流程图3 ATF BL1 ufs初始化简单过程分析3.1 调用过程3.2 hikey960_ufs_init3.3 dw_ufs_init3.3 ufs_init 以海思hikey960为例来介绍&#xff0c;简单介绍在ATF BL1阶段的初始化处理。 1 ATF的下载链接 https:/…

【ARM 嵌入式 编译系列 10.2 -- 符号表与可执行程序分离详细讲解】

文章目录 符号表与可执行程序分离方法一 使用eu-strip方法二 使用 objcopy上篇文章:ARM 嵌入式 编译系列 10.1 – GCC 编译缩减可执行文件 elf 文件大小 下篇文章:ARM 嵌入式 编译系列 10.3 – GNU elfutils 工具小结 符号表与可执行程序分离 接着上篇文章 ARM 嵌入式 编译…

Amazon EMR Hudi 性能调优——Clustering

随着数据体量的日益增长&#xff0c;人们对 Hudi 的查询性能也提出更多要求&#xff0c;除了 Parquet 存储格式本来的性能优势之外&#xff0c;还希望 Hudi 能够提供更多的性能优化的技术途径&#xff0c;尤其当对 Hudi 表进行高并发的写入&#xff0c;产生了大量的小文件之后&…

Vue.js快速入门指南:零基础也能轻松上手,开启前端开发之旅!

目录 MVC设计模式与MVVM设计模式选项式API的编程风格与优势声明式渲染及响应式数据实现原理指令系统与事件方法及传参处理计算属性与侦听器区别与原理条件渲染与列表渲染及注意点class样式与style样式的三种形态表单处理与双向数据绑定原理生命周期钩子函数及原理分析 MVC设计模…

python安装第三方包时报错:...\lib\site-packages\pip\_vendor\urllib3\response.py...

安装redis第三方包&#xff1a; pip install redis报错现象&#xff1a; 解决方法&#xff1a;使用以下命令可成功安装 pip install redis -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

小白到运维工程师自学之路 第七十三集 (kubernetes应用部署)

一、安装部署 1、以Deployment YAML方式创建Nginx服务 这个yaml文件在网上可以下载 cat nginx-deployment.yaml apiVersion: apps/v1 #apiVersion是当前配置格式的版本 kind: Deployment #kind是要创建的资源类型&#xff0c;这里是Deploymnet metadata: #metadata是该资源…

kubernetes中PV和PVC

目录 一、PV、PVC简介 二、PV、PVC关系 三、创建静态PV 1.配置nfs存储 2.定义PV 3.定义PVC 4.测试访问 四、 搭建 StorageClass nfs-client-provisioner &#xff0c;实现 NFS 的动态 PV 创建 1. 配置nfs服务 2.创建 Service Account 3.使用 Deployment 来创建 NFS P…