C++11新特性-4大类型转换

本文详细介绍了C++中的四种类型转换操作符:static_cast、reinterpret_cast、dynamic_cast和const_cast。static_cast用于基本数据类型和类层次结构之间的安全转换;reinterpret_cast执行低级别的类型转换,如数值与指针间的转换;dynamic_cast提供运行时类型检查,用于安全的基类与派生类转换;const_cast主要用于去除变量的const属性。文章还给出了各种转换的示例代码,强调了类型转换的注意事项和建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.C++类型转换总述

  • 旧式转型 C风格的强制类型:

TYPE b = (TYPE) a
例如:
int i = 48;
char c = (char) i;

  • 新式转型C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。

格式:
TYPE b = 类型操作符< TYPE> ( a )
类型操作符= static_cast | reinterpreter_cast | dynamic_cast | const_cast

例:
static_cast与reinterpreter_cast就可以替换掉旧式风格转换

double PI = 3.14159;
int i1 = (int)PI;
int i2 = static_cast<int> (PI);

int *addr1 = (int*)(0x888888);
int *addr2 = reinterpreter_cast<int*> (0x888888);

①static_cast

静态类型转换(斯文的劝导,温柔的转换)。如int转换成char,隐式转换

主要用法:

  • 用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换上行指针或引用(派生类到基类)转换安全,下行不安全
  • 用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性也要开发人员来保证。
  • 空指针转换成目标类型的空指针。,如int *addr = (int *) 0x88888;
  • 任何类型的表达式转换成void类型。

定义一个Animal抽象类,定义纯虚函数cry。定义Cat和Dog继承自抽象类,并实现cry。
以下用例:

  1. 子类的指针转型到父类指针、父类的指针转型到子类的指针(危险)、子类的引用转型到父类的引用\父类到子类引用
  2. 把空指针转换成目标类型的空指针、把任何类型的表达式转换成void类型
#include <iostream>
using namespace std;

class Animal {
public:
	virtual void cry() = 0;
};

class Cat :public Animal
{
public:
	void cry()
	{
		cout << "喵喵瞄" << endl;
	}
	
};

class Dog :public Animal
{
public:
	void cry()
	{
		cout << "汪汪汪" << endl;
	}

};

int main(void) {


	//第一种情况  父子类之间的类型转换
	Dog* dog1 = new Dog();
	Animal* a1 = static_cast<Animal*>(dog1); //子类的指针转型到父类指针

	Dog* dog1_1 = static_cast<Dog*>(a1);     //父类的指针转型到子类的指针
	Cat* cat1 = static_cast<Cat*>(a1);       //父类到子类,有风险,这样是不行的,会出问题

	Dog dog2;
	Animal& a2 = static_cast<Animal&>(dog2); //子类的引用转型到父类的引用
	Dog &dog2_2 = static_cast<Dog&>(a2);     //父类到子类引用

	//第二种 基本类型的转换
	int  kk = 234;
	char cc = static_cast<char>(kk);

	//第三种 把空指针转换成目标类型的空指针。
	int* p = static_cast<int*>(NULL);
	Dog* dp = static_cast<Dog*>(NULL);

	//第四种 把任何类型的表达式转换成void类型
	int* pi = new int[10];
	void* vp = static_cast<void*>(pi);
	vp = pi;

	system("pause");
	return 0;
}

总结:同类型之间的转换。

②reinterpret_cast

重新解释类型(挂羊头,卖狗肉) 不同类型间的互转,数值与指针间的互转

用法: TYPE b = reinterpret_cast < TYPE> ( a )
TYPE必须是一个指针、引用、算术类型、函数指针.

忠告:滥用 reinterpret_cast 运算符可能很容易带来风险除非所需转换本身是低级别的,否则应使用其他强制转换运算符之一。

定义一个Animal基类,定义成员函数cry。定义Cat和Dog继承自基类,并重写cry。
以下用例:
数值与指针之间的转换
不同类型指针和引用之间的转换

#include <iostream>

using namespace std;

class Animal {
public:
	void cry() {
		cout << "动物叫" << endl;
	}
};

class Cat :public Animal
{
public:
	void cry()
	{
		cout << "喵喵瞄" << endl;
	}

};

class Dog :public Animal
{
public:
	void cry()
	{
		cout << "汪汪汪" << endl;
	}

};

int main(void) {

	//用法一   数值与指针之间的转换
	int* p = reinterpret_cast<int*>(0x99999);
	int val = reinterpret_cast<int>(p);

	//用法二  不同类型指针和引用之间的转换
	Dog  dog1;
	Animal* a1 = &dog1;
	a1->cry();

	Dog* dog1_p = reinterpret_cast<Dog*>(a1);
	Dog* dog2_p = static_cast<Dog*>(a1);   //如果能用static_cast ,static_cast 优先

	//Cat* cat1_p = static_cast<Cat*>(a1);
	//Cat* cat2_p = static_cast<Cat*>(dog1_p);//NO! 不同类型指针转换、不是继承关系不能使用static_cast
	Cat* cat2_p = reinterpret_cast<Cat*>(dog1_p);	//猫与狗强行转换

	Animal& a2 = dog1;
	Dog& dog3 = reinterpret_cast<Dog&>(a2);//引用强转用法
	
	dog1_p->cry();
	dog2_p->cry();

	cat2_p->cry();


	system("pause");
	return 0;
}

总结:不同类型之间的转换。并且注意如果能用static_cast ,static_cast 优先.

③dynamic_cast

动态类型转换

  • 将一个基类对象指针cast到继承类指针,dynamic_cast
    会根据基类指针是否真正指向继承类指针来做相应处理。失败返回null,成功返回正常cast后的对象指针;
  • 将一个基类对象引用cast 继承类对象,dynamic_cast
    会根据基类对象是否真正属于继承类来做相应处理。失败抛出异常bad_cast

注意:dynamic_cast在将父类cast到子类时,父类必须要有虚函数一起玩。

定义一个Animal抽象类,定义纯虚函数cry。定义Cat和Dog继承自抽象类,并实现cry,添加play成员函数。定义形参为类的引用和指针的普通函数void animalPlay(Animal& animal),void animalPlay(Animal* animal)

#include <iostream>

using namespace std;

class Animal {
public:
	virtual void cry() = 0;
};

class Cat :public Animal
{
public:
	void cry()
	{
		cout << "喵喵瞄" << endl;
	}

	void play()
	{
		cout << "爬爬树"<<endl;
	}

};

class Dog :public Animal
{
public:
	void cry()
	{
		cout << "汪汪汪" << endl;
	}

	void play()
	{
		cout << "溜达溜达" << endl;
	}

};

void animalPlay(Animal& animal) {
	animal.cry();	//多态
	
	//会根据基类对象是否真正属于继承类来做相应处理**。失败抛出异常bad_cast
	try {
		Dog& pDog = dynamic_cast<Dog&>(animal);
		pDog.play();
	}
	catch (std::bad_cast bc) {
		cout << "不是狗,那应该是猫" << endl;
	}

	try {
		Cat& pCat = dynamic_cast<Cat&>(animal);
		pCat.play();
	}
	catch (std::bad_cast bc) {
		cout << "不是猫,那应该是上面的狗" << endl;
	}

}


void animalPlay(Animal* animal) {
	animal->cry();	//多态

	//根据基类指针是否真正指向继承类指针来做相应处理**。失败返回null,成功返回正常cast后的对象指针
	Dog* pDog = dynamic_cast<Dog*>(animal);
	if (pDog) {
		pDog->play();
	}
	else {//pDog == NULL
		cout << "不是狗,别骗我!" << endl;
	}


	Cat* pCat = dynamic_cast<Cat*>(animal);
	if (pCat) {
		pCat->play();
	}
	else {//pDog == NULL
		cout << "不是猫,别骗我!" << endl;
	}
}


int main(void) {

	Dog* dog1 = new Dog();
	Animal* a1 = dog1;

	//animalPlay(a1);

	Dog dog2;
	animalPlay(dog2);

	Cat* cat1 = new Cat();
	Animal* a2 = cat1;

	//animalPlay(a2);
	Cat cat2;
	animalPlay(cat2);

	system("pause");
	return 0;
}

③const_cast

去const属性。(仅针对于指针和引用)

#include <iostream>
using namespace std;

void demo(const char* p)
{
	//对指针去掉const 重新赋值
	//char* p1 = const_cast<char*>(p);
	//p1[0] = 'A';

	//直接去掉const修改
	const_cast<char*>(p)[0] = 'A';

	
	cout << p << endl;

}

//没有必要
void demo(const int p)
{
	int q = p;
	//const_cast<int>(p) = 888;// NO ! 不能对非指针和引用进行const 转换
	cout << p << endl;
}


int main(void)
{
	//字符串数组
	//char p[] = "12345678";
	//demo(p);  //合情合理

	//常量字符串不能去掉const修改
	//警告: 在去掉常量限定符之前,保证指针所指向的内存能够修改,不能修改则会引起异常。
	const char* cp = "987654321";
	demo(cp);//会报错

	system("pause");
	return 0;
}

2.类型转换使用建议

  1. static_cast静态类型转换,编译的时c++编译器会做编译时的类型检查隐式转换;基本类型转换,父子类之间合理转换
  2. 不同类型之间,进行强制类型转换,用reinterpret_cast<>() 进行重新解释
    建 议:
    C语言中 能隐式类型转换的,在c++中可用 static_cast<>()进行类型转换。因C++编译器在编译检查一般都能通过;C语言中不能隐式类型转换的在c++中可以用 reinterpret_cast<>() 进行强制类型解释。

总结:static_cast<>()和reinterpret_cast<>() 基本上把C语言中的 强制类型转换给覆盖,注意reinterpret_cast<>()很难保证移植性。

  1. dynamic_cast<>(),动态类型转换,安全的虚基类和子类之间转换;运行时类型检查
  2. const_cast<>()去除变量的只读属性

最后的忠告:程序员必须清楚的知道: 要转的变量,类型转换前是什么类型,类型转换 后是什么类型,转换后有什么后果。

建议:一般情况下,不建议进行类型转换;避免进行类型转换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值