[STL] C ++ type conversion static_cast, dynamic_cast, const_cast, reinterpret_cast

C ++ and C language type conversion there is a difference in the C language in general just do not cast their security checks. But in C ++, we must not only be type conversion, the system will be subjected to security checks, so we used the following four key type conversion in C ++:
1-static_cast general type conversion
2-dynamic_cast usually base use of derived class switching between
3-const_cast aimed const conversion
4-reinterpret_cast for no association between any conversion, such as a character pointer to an integer number.

#include<iostream>
using namespace std;

//static_cast 一般类型转换
//dynamic_cast 通常在基类和派生类之间转换时使用
//const_cast 主要针对const的转换
//reinterpret_cast 用于进行没有任何关联之间的转换,比如一个字符指针转换为一个整型数。

class Building {

};
class Animal {

};
class Cat :public Animal {

};


/////////////////////////////////////*****************************///////////////////////////
//1-static_cast 一般类型转换
void test01() {
	int a = 97;
	char c = static_cast<char>(a);
	cout << c << endl;

	//基础数据类型指针
	//int* p = NULL;
	//char* cp = static_cast<char*>(p);//基础数据指针类型不能转。
	
	// 对象指针
	//Building* building = NULL;
	//Animal* ani = static_cast<Animal*>(building);//对象指针类型不能转。

	//转换具有继承关系的对象指针
	//父类指针可以转换成之类指针
	Animal* ani = NULL;
	Cat* cat = static_cast<Cat*>(ani);
	//子类指针转换成父类指针
	Cat* soncat = NULL;
	Animal* anifather = static_cast<Animal*>(soncat);

	Animal aniobj;
	Animal& aniref = aniobj;
	Cat& cat1 = static_cast<Cat&>(aniref);

	Cat catobj;
	Cat& catref = catobj;
	Animal& anifather2 = static_cast<Animal&>(catref);

	//static_cast  用于内置的数据类型,还具有继承关系的指针或者引用。
}
////////////////////////////////************************/////////////////////////////////////
//2-dynamic_cast 转换具有继承关系的指针或者引用,在转换前会进行对象类型检查
void test02() {
	//基础数据类型
	//int a = 10;
	//char c = dynamic_cast<char>(a);//基础数据类型不能转换。

	//非继承关系指针
	//Animal* ani = NULL;
	//Building* building = dynamic_cast<Building*>(ani);//非继承关系指针不能转换
	
	//具有继承关系指针
	//Animal* ani = NULL;
	//Cat* cat = dynamic_cast<Cat*>(ani);//这是父类指针转换成子类指针,不安全。系统不让转。
	//原因在于dynamic做类型安全检查。

	Cat* cat2 = NULL;
	Animal* ani = dynamic_cast<Animal*>(cat2);//这是子类指针转换成父类指针,安全。

	//结论:dynamic只能转换具有继承关系的指针或者引用,并且只能由子类类型转成基类型。
}
///////////////////////////////////********************************/////////////////////////////////////
//3-const_cast  针对指针、引用、对象指针的const属性的去掉。
void test03() {
	//1-基础数据类型
	int a = 10;
	const int& b = a;
	//b = 10;//此时b是不能被修改的。
	int& c = const_cast<int&>(b);
	c = 20;
	cout << "a:" << a << endl;
	cout << "b:" << a << endl;
	cout << "c:" << a << endl;

	//看指针
	const int* p = NULL;
	int *p2=const_cast <int*>(p);

	int* p3 = NULL;
	const int* p4 = const_cast<const int*>(p3);

	//const_cast 增加或者去除变量const性。
}
/////////////////////////////////////*****************************************/////////////////////////////////////////////
//4-reinterpret_cast 强制类型转换(任何类型)

typedef void(*FUNC)(int, int);
typedef int(*FUNC2)(int, char*);
void test04() {
	//(1)无关的指针类型都可以进行转换。
	Building* building = NULL;
	Animal* ani = reinterpret_cast<Animal*>(building);

	//(2)函数指针转换
	FUNC func1;
	FUNC2 = reinterpret_cast<FUNC2>(func1);

}


int  main() {
	test01();
	test03();
	return 0;
}

///////////////////***********总结论***********/////////////////////
/*
结论1:程序员必须清楚的知道要转变的变量,转换前是什么类型,转换后是什么类型,以及转换后有什么后果。
结论2:一般情况下,不建议类型转换,避免进行类型转换。
*/
Published 57 original articles · won praise 28 · views 4138

Guess you like

Origin blog.csdn.net/weixin_41747893/article/details/102778862