61.C++类型转换

本文深入探讨了C++中的四种类型转换:静态转换(static_cast)、动态转换(dynamic_cast)、常量转换(const_cast)和重新解释转换(reinterpret_cast),并详细说明了它们的使用场景和安全性。静态转换适用于基本数据类型和类层次结构之间的转换,但下行转换不安全;动态转换增加了类型检查,提高了安全性;常量转换用于去除或添加const属性;重新解释转换最不安全,不推荐使用。

静态转换(static_cast)

用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。

  • 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的;
  • 进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。

用于基本数据类型之间的转换,如把int转换成char,把char转换成int。这种转换的安全性也要开发人员来保证
静态转换 static_cast
使用方式  static_cast< 目标类型>(原始数据)
可以进行基础数据类型转换 
父与子类型转换
没有父子关系的自定义类型不可以转换

class Animal{};
class Dog : public Animal{};
class Other{};

//基础数据类型转换
void test01(){
	char a = 'a';
	double b = static_cast<double>(a);
}

//继承关系指针互相转换
void test02(){
	//继承关系指针转换
	Animal* animal01 = NULL;
	Dog* dog01 = NULL;
	//子类指针转成父类指针,安全
	Animal* animal02 = static_cast<Animal*>(dog01);
	//父类指针转成子类指针,不安全
	Dog* dog02 = static_cast<Dog*>(animal01);
}

//继承关系引用相互转换
void test03(){

	Animal ani_ref;
	Dog dog_ref;
	//继承关系指针转换
	Animal& animal01 = ani_ref;
	Dog& dog01 = dog_ref;
	//子类指针转成父类指针,安全
	Animal& animal02 = static_cast<Animal&>(dog01);
	//父类指针转成子类指针,不安全
	Dog& dog02 = static_cast<Dog&>(animal01);
}

//无继承关系指针转换
void test04(){
	
	Animal* animal01 = NULL;
	Other* other01 = NULL;

	//转换失败
	//Animal* animal02 = static_cast<Animal*>(other01);
}

动态转换(dynamic_cast)

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

动态转换 dynamic_cast

不可以转换基础数据类型
父子之间可以转换

  • 父转子 不可以
  • 子转父 可以
  • 发生多态 都可以
class Animal {
public:
	virtual void ShowName() = 0;
};
class Dog : public Animal{
	virtual void ShowName(){
		cout << "I am a dog!" << endl;
	}
};
class Other {
public:
	void PrintSomething(){
		cout << "我是其他类!" << endl;
	}
};

//普通类型转换
void test01(){

	//不支持基础数据类型
	int a = 10;
	//double a = dynamic_cast<double>(a);
}

//继承关系指针
void test02(){

	Animal* animal01 = NULL;
	Dog* dog01 = new Dog;

	//子类指针转换成父类指针 可以
	Animal* animal02 = dynamic_cast<Animal*>(dog01);
	animal02->ShowName();
	//父类指针转换成子类指针 不可以
	//Dog* dog02 = dynamic_cast<Dog*>(animal01);
}

//继承关系引用
void test03(){

	Dog dog_ref;
	Dog& dog01 = dog_ref;

	//子类引用转换成父类引用 可以
	Animal& animal02 = dynamic_cast<Animal&>(dog01);
	animal02.ShowName();
}

//无继承关系指针转换
void test04(){
	
	Animal* animal01 = NULL;
	Other* other = NULL;

	//不可以
	//Animal* animal02 = dynamic_cast<Animal*>(other);
}

常量转换(const_cast)

该运算符用来修改类型的const属性。。

  • 常量指针被转化成非常量指针,并且仍然指向原来的对象;
  • 常量引用被转换成非常量引用,并且仍然指向原来的对象;

注意:不能直接对非指针和非引用的变量使用const_cast操作符去直接移除它的const.
 

//常量指针转换成非常量指针
void test01(){
	
	const int* p = NULL;
	int* np = const_cast<int*>(p);

	int* pp = NULL;
	const int* npp = const_cast<const int*>(pp);

	const int a = 10;  //不能对非指针或非引用进行转换
	//int b = const_cast<int>(a); }

//常量引用转换成非常量引用
void test02(){

int num = 10;
	int & refNum = num;

	const int& refNum2 = const_cast<const int&>(refNum);
	
}

main.cpp

// 43.模板.cpp : 定义控制台应用程序的入口点。
//

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

//静态转换
//基础类型

void test01()
{
	char a = 'a';

	double d = static_cast<double>(a);

	cout << "d = " << d << endl;//d = 97
}

//父子之间转换
class Base{};
class Child :public Base{};
class Other{};
void test02()
{
	Base * base = NULL;
	Child * child = NULL;

	//把base转为 Child*类型 向下  不安全
	Child * child2 = static_cast<Child*>(base);

	//把child 转为 Base*  向上  安全
	Base * base2 = static_cast<Base*>(child);

	//转other类型 转换无效
	//Other * other = static_cast<Other*>(base);
}

//static_cast使用   static_cast<目标类型>(原始对象)

// 动态转换

void test03()
{
	//基础类型不可以转换
	char c = 'a';
	//dynamic_cast非常严格,失去精度 或者不安全都不可以转换
	//double d = dynamic_cast<double>(c);

}

class Base2
{
	virtual void func(){};
};
class Child2 :public Base2
{
	virtual void func(){};
};
class Other2{};

void test04()
{
	Base2 * base = NULL;
	Child2 * child = NULL;

	//child转Base2 *  安全
	Base2 * base2 = dynamic_cast<Base2*>(child);


	//base 转Child2 * 不安全
	//Child2 * child2 = dynamic_cast<Child2*>(base);

	//dynamic_cast 如果发生了多态,那么可以让基类转为派生类 ,向下转换
	Base2 * base3 = new Child2;
	Child2 * child3 = dynamic_cast<Child2*>(base3);

}


// 常量转换(const_cast)
void test05()
{
	const int * p = NULL;
	//取出const
	int * newp = const_cast<int *>(p);

	int * p2 = NULL;
	const int * newP2 = const_cast<const int *>(p2);


	//不能对非指针 或 非引用的 变量进行转换
	//const int a = 10;
	//int b = const_cast<int>(a);

	//引用
	int num = 10;
	int &numRef = num;

	const int &numRef2 = static_cast<const int &>(numRef);
}

//重新解释转换(reinterpret_cast)
void test06()
{
	int a = 10;
	int * p = reinterpret_cast<int *>(a);

	Base * base = NULL;
	Other * other = reinterpret_cast<Other*>(base);

	//最不安全 ,不推荐
}


int main(){
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值