`

C++四个类型转换关键字const_cast, static_cast, dynamic_cast, reinterpret_cast

 
阅读更多

C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:
TYPE b = (TYPE)a。


C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。
const_cast,字面上理解就是去const属性。
static_cast,命名上理解是静态类型转换。如int转换成char。
dynamic_cast,命名上理解是动态类型转换。如子类和父类之间的多态类型转换。
reinterpret_cast,仅仅重新解释类型,但没有进行二进制的转换。
4种类型转换的格式,如:TYPE B = static_cast(TYPE)(a)。

const_cast
去掉类型的const或volatile属性。

static_cast
类似于C风格的强制转换。无条件转换,静态类型转换。用于:
1. 基类和子类之间转换:其中子类指针转换成父类指针是安全的;但父类指针转换成子类指针是不安全的。(基类和子类之间的动态类型转换建议用dynamic_cast)
2. 基本数据类型转换。enum, struct, int, char, float等。static_cast不能进行无关类型(如非基类和子类)指针之间的转换。
3. 把空指针转换成目标类型的空指针。
4. 把任何类型的表达式转换成void类型。
5. static_cast不能去掉类型的const、volitale属性(用const_cast)。

dynamic_cast
有条件转换,动态类型转换,运行时类型安全检查(转换失败返回NULL):
1. 安全的基类和子类之间转换。
2. 必须要有虚函数。
3. 相同基类不同子类之间的交叉转换。但结果是NULL。

reinterpret_cast
仅仅重新解释类型,但没有进行二进制的转换:
1. 转换的类型必须是一个指针、引用、算术类型、函数指针或者成员指针。
2. 在比特位级别上进行转换。它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针(先把一个指针转换成一个整数,在把该整数转换成原类型的指针,还可以得到原先的指针值)。但不能将非32bit的实例转成指针。
3. 最普通的用途就是在函数指针类型之间进行转换。
4. 很难保证移植性。

总结
去const属性用const_cast。
基本类型转换用static_cast。
多态类之间的类型转换用daynamic_cast。
不同类型的指针类型转换用reinterpret_cast。

 

参考文档:http://www.cnblogs.com/goodhacker/archive/2011/07/20/2111996.html

 

程序示例:

#include<iostream>
using namespace std;

class B{
public:
	int m_iNum;
	B():m_iNum(50){}
};

class BaseClass{
public:
	int m_iNum;
	virtual void foo(){} //基类必须有虚函数,保持多态特性才能使用dynamic_cast
};

class DerivedClass:public BaseClass{
public:
	char* m_szdName[100];
	void bar(){}
};

void const_cast_test(){
	/*
		const_cast<type>(exp) 用法:
			该运算符用来修改类型的const后volatile属性。除了const或volatile修饰之外,type_id和expression的类型是一样的。
	*/

	//1. 常量指针(指向的内容不能改变,见我的blog)被转化为非常量指针,并仍然指向原来的对象
	const B* b1=new B();
	//b1->m_iNum=100; //compile error
	B* b2=const_cast<B*>(b1);
	b2->m_iNum=200;
	cout<<"b1: "<<b1->m_iNum<<endl;
	cout<<"b2: "<<b2->m_iNum<<endl;
	cout<<endl;

	//2. 常量对象被转化为非常量对象,产生新的对象
	const B b3;
	// b3.m_iNum=100; //compile error
	B b4=const_cast<B&>(b3);	//注:右边const_cast<B&>(b3)是引用;b4是对右边的复制 
	b4.m_iNum=201;
	cout<<"b3: "<<b3.m_iNum<<endl;
	cout<<"b4: "<<b4.m_iNum<<endl;
	cout<<endl;

	//3. 常量引用转为为非常量引用,并仍然指向原来的对象
	const B b5;
	B& b6=const_cast<B&>(b5);	//注:右边const_cast<B&>(b5)是引用;b6是对右边的引用 
	b6.m_iNum=202;
	cout<<"b5: "<<b5.m_iNum<<endl;
	cout<<"b6: "<<b6.m_iNum<<endl;
	cout<<endl;
	
	cout<<"下面不是bug:因为在定义const时,就把其作为一个常量。换言之,如果你接收一个程序,有人定义了一个常量,你碰巧用到,但需要在某处修改,而又不能去改变之前程序用到该数据的地方,那么其他地方跟着你变吗?显然不可以。如果你想用,最好定义一个指针指向存储这个数的地方,每次通过读取指针并对指针赋值就没问题了。"<<endl;
	cout<<"const int ic = 100"<<endl;
	cout<<"int *pt = const_cast<int*>(&ic);"<<endl;
	cout<<"这个*pt就是你需要的变量了"<<endl<<endl;
	const int b7=7;
	int b8 = const_cast<int&>(b7);
	b8=8;
	cout<<"b7: "<<b7<<endl;
	cout<<"b8: "<<b8<<endl;
	
	const int b9=9;
	int& b10 = const_cast<int&>(b9);
	b10=10;
	cout<<"b9: "<<b9<<endl;
	cout<<"b10: "<<b10<<endl;
	
	cout<<endl<<"---------------------------"<<endl;
}

void static_cast_test(){
	//类似C风格强制转换. 命名上理解是静态类型转换。如int转换成char
	int n=6;
	double d=static_cast<double>(n);
	cout<<d<<endl;

	void* p=static_cast<void*>(&n);

	cout<<endl<<"---------------------------"<<endl;
}

void dynamic_cast_test(){
	//3. 命名上理解是动态类型转换(如子类和父类之间的多态类型转换).
	//注:运行时类型安全检查,转换失败返回NULL
	 BaseClass* pb = new DerivedClass();
	 DerivedClass *pd1 = static_cast<DerivedClass *>(pb); //子类->父类,静态类型转换,正确但不推荐
	 DerivedClass *pd2 = dynamic_cast<DerivedClass *>(pb); //子类->父类,动态类型转换,正确
	 cout<<pd1<<","<<pd2<<endl;
 
	BaseClass* pb2 = new BaseClass();
	DerivedClass *pd21 = static_cast<DerivedClass *>(pb2); //父类->子类,静态类型转换,危险!访问子类m_szName成员越界
	DerivedClass *pd22 = dynamic_cast<DerivedClass *>(pb2); //父类->子类,动态类型转换,安全的。结果是NULL
	cout<<pd21<<","<<pd22<<endl;

	cout<<endl<<"---------------------------"<<endl;
}

int doSth(){
	cout<<"doSth()"<<endl;
	return 0;
}

void reinterpret_cast_test(){
	//4. 重新解释类型,常用来在函数指针类型之间进行转换
	typedef void(*FuncPtr)();//FuncPtr is 一个指向函数的指针,该函数没有参数,返回值类型为 void
	FuncPtr funcPtrArray[10];//10个FuncPtrs指针的数组 让我们假设你希望(因为某些莫名其妙的原因)把一个指向下面函数的指针存入funcPtrArray数组:

	//funcPtrArray[0]=&doSth;
	funcPtrArray[0]=reinterpret_cast<FuncPtr>(&doSth);// 编译错误!类型不匹配,reinterpret_cast可以让编译器以你的方法去看待它们:funcPtrArray
	funcPtrArray[0]();//不同函数指针类型之间进行转换
	
	cout<<endl<<"---------------------------"<<endl;
}

int main(){
	const_cast_test();
	static_cast_test();
	dynamic_cast_test();
	reinterpret_cast_test();
	return 0;
}

 

结果截图:

 

  • 大小: 46.9 KB
分享到:
评论

相关推荐

    标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast

    标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast

    static_cast、dynamic_cast、reinterpret_cast和const_cast

    标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast

    C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast总结

    主要介绍了C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast总结,需要的朋友可以参考下

    C++中4种强制类型转换的区别总结

    使用标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast。 const_cast,字面上理解就是去const属性。 static_cast,命名上理解是静态类型转换。如int转换成char。 dynamic_cast,...

    static_cast,dynamic_cast,reinterpret_cast,const_cast的区别及用法详解

    2.dynamic_cast提供安全的转换如果两个指针不存在继承关系转换会失败返回空指针,如果你提供一个错误的指针那样会发生内存访问异常,因为它会去比较两个类型的虚函数表。虚函数表的指针一般放在对象指针最开始的四字...

    C++中用于强制类型转换的四个运算符

    本文详细介绍了C++中的四个用与强制类型转换的运算符:用来修改类型的const 或volatile 属性的const_cast,用来修改操作数类型的reinterpret_cast,static_cast,dynamic_cast

    C++中的类型转换

    C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast

    C++关键字大全(67个).txt

    C++关键字大全(67个) asm auto bad_cast bad_typeid bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum except explicit extern false finally float ...

    C++中4种类型转换方式 cast操作详解

    什么是static_cast,dynamic_cast以及reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变一个变量的类型为别的类型从而改变该变量的表示方式。为了类型转换一个简单对象为另一个对象你会使用传统...

    关于C++的强制类型转换浅析

    C++其实也具有自己的一套强制类型转换它们分明是:static_cast reinterpret_cast const_cast dynamic_cast四种类型. 那么肯定会有人好奇C++是不是闲,C语言的强制类型用的舒舒服服的,为什么要新推出来这几个? 新...

    C++ 中的强制类型转换

    C++ 中的强制类型转换,显示转换也成为强制类型转换(cast),包括以下列名字命名的强制类型转换操作符:static_cast、dynamic_cast、const_cast、reinterpret_cast。

    深入C++四种强制类型转换的总结

    c++中提供了四种新的强制转换分别是:const_cast、dynamic_cast、reinterpret_cast、static_cast.这四种转换类型,每一种都适用于特定的目的:const_cast 一般用于强制取消对象的常量性。它是唯一能够做到这一点的...

    C++中的四种类型转换

    总所周知,在C++ 当中引入了四种新的类型转换操作符:static_cast, dynamic_cast, reinterpret_cast,还有const_cast。就自己见过的一些C++代码当中,它们的使用其实并不普遍。不少程序员依然乐于去使用C-like的类型...

    C++面试常问知识点总结

    C++更加安全,增加了const常量、引用、四类cast转换(static_cast、dynamic_cast、const_cast、reinterpret_cast)、智能指针、try—catch等等; C++可复用性高,C++引入了模板的概念,后面在此基础上,实现了方便...

    C++类型转换的深入总结

    dynamic_cast,命名上理解是动态类型转换。如子类和父类之间的多态类型转换。 reinterpret_cast,仅仅重新解释类型,但没有进行二进制的转换。 4种类型转换的格式,如:TYPE B = static_cast(TYPE)(a)。 const_cast ...

    C++中四种强制类型转换的区别

    使用标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast。  1、static_cast  用法:static_cast (expression)  该运算符把expression转换为type-id类型,但没有运行时类型检查来...

    C++四种强制类型转换

    四种强制类型转换:static_cast const_cast dynamic_cast reinterpret_cast  1、static_cast  编译器隐式执行的任何类型转换都可以由static_cast显示完成。  ①用于类层次结构中基类和子类之间指针或引用的...

    C++关键字详细使用

    char class const const_cast continue default delete do double dynamic_cast else enum except explicit extern false finally float for friend goto if inline int long mutable namespace new operator ...

    解析C++中四种强制类型转换的区别详解

    分别为:static_cast , dynamic_cast , const_cast , reinterpret_cast为什么使用C风格的强制转换可以把想要的任何东西转换成合乎心意的类型。那为什么还需要一个新的C++类型的强制转换呢?新类型的强制转换可以提供...

Global site tag (gtag.js) - Google Analytics