C++的四种类型转换

文章目录

const_cast:去掉常量类型的类型转换

const_cast<>是语言级别上的检测,和C风格的强制转换在汇编代码上是一样的

c 复制代码
//C
const int a1 = 10;
double* p1 = (double*)&a1;
cpp 复制代码
//C++
int* p2 = const_cast<int*>(&a1);

const_cast只能用于去掉const属性,不弄用于类型转换

且const_cast<里面只能放指针和引用类型>

static_cast:提供编译器认为安全的类型转换(在编译阶段完成类型转换)

static_cast允许标准类型之间的相互转换和void*类型转换成其他指针类型

cpp 复制代码
int a = 10;
char b = static_cast<char>(a);
long c = static_cast<long>(a);
cpp 复制代码
int* p = nullptr;
short* b = static_cast<short*>(p);//错误

void* p1 = nullptr;
short* p2 = static_cast<short*>(p1);

reinterpret:类似c风格的强制类型转化

cpp 复制代码
int* k = nullptr;
//危险!容易照成未定义行为
short* b = reinterpret_cast<short*>(k);
double* j = reinterpret_cast<double*>(k);

原本指向四字节的int型k指针被强制转成八字节,多出那四字节可是未初始化的空间,当程序使用这块空间时会导致程序宕机

dynamic_cast:主要用在继承结构里,可以支持RTTI类型识别的上下转换

在程序运行时动态检查对象的实际类型。它会查询对象的RTTI信息(存储在虚函数表中)来判断转换是否合法

cpp 复制代码
class A
{
public:
	virtual void func() = 0;
};

class B : public A
{
public:
	void func() { std::cout << "call B::func" << std::endl; }
};

class C : public A
{
public:
	void func() { std::cout << "call C::func" << std::endl; }
	void C_uinque_func() {std::cout << "C unique func" << std:: endl; }
};

int main()
{

	B b1;
	C c1;

	show_func(&b1);
	show_func(&c1);

	return 0;
}

写一个show_func函数

cpp 复制代码
void show_func(A* p)
{
	p->func();
}

int main()
{

	B b1;
	C c1;

	show_func(&b1);
	show_func(&c1);

	return 0;
}

通过虚函数调用到两个派生类重写以后的函数的打印结果

当我们想实现传入子类C执行 C_uinque_func()函数

传入子类B执行func()函数时

可以把show_func()函数改成这样

cpp 复制代码
void show_func(A* p)
{
	C* pd2 = dynamic_cast<C*>(p);
	//若转换非法(B无法转换成C,这两个之间无继承关系)对指针返回nullptr
	if(pd2 != nullptr)
	{
		pd2->C_uinque_func();
	}
	else
	{
		p->func();
	}
}

int main()
{

	B b1;
	C c1;

	show_func(&b1);
	show_func(&c1);

	return 0;
}

此时就可以动态调用到子类C的函数

dynamic_cast<>仅对多态类型(含虚函数的类)有效

如果我们把基类A里的虚函数改为普通函数,

dynamic无法正常使用

多继承里的交叉转换

dynamic_cast 可以用于跨越不同基类分支的转换,前提是目标类型与对象实际类型兼容

cpp 复制代码
class Base1 
{
public:
	Base1(){std::cout << "this is Base1" << std::endl;}
	virtual ~Base1() {} 
};
class Base2 
{ 
public: 
	Base2(){std::cout << "this is Base2" << std::endl;}
	virtual ~Base2() {} 
};
class Derived : public Base1, public Base2 
{

};

int main()
{

	Base1* b1 = new Derived;
	// 合法,因为对象实际是Derived
	Base2* b2 = dynamic_cast<Base2*>(b1);

	return 0;
}

在类的转换时,在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的。在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全

相关推荐
端平入洛1 天前
delete又未完全delete
c++
端平入洛2 天前
auto有时不auto
c++
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1233 天前
matlab画图工具
开发语言·matlab
dustcell.3 天前
haproxy七层代理
java·开发语言·前端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20213 天前
信号量和信号
linux·c++
多恩Stone3 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054963 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月3 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js