类型转换static_cast和reinterpreter_cast

心已入冬 提交于 2020-01-31 00:17:58

类型转换名称和语法

c风格的强制转换 TYPE b = (TYPE)a;

c++风格的类型转换提供了四种类型转换操作符

  1. static_cast 静态类型转换
  2. reinterpreter_cast 重新解释类型
  3. dynamic_cast 动态类型转换,如子类和父类之间的多态类型转换
  4. const_cast 字面上理解就是去const属性
  5. 四种类型转换的格式:TYPE B = static_cast(a);

总结
通过static_cast和reinterpreter_cast吧C语言的强制类型转换覆盖了
代码案例

void main()
{	
	double a = 32.46546;
 	int b = (int)a;//C类型转换
	int c = static_cast<int>(a);//C++静态类型转换 编译的时候编译器会做类型检查
 	int d = a;//C语言中,隐式类型转换的地方,均可使用static_cast进行类型转换
 	//char*==>int*
 {
  	char *a = "ajdhk";
 	int *b = reinterpret_cast<int*>(a);
  	cout<<"a: "<<a<<endl;//打印出指针变量所指向的内存空间的	值
 	cout<<"b: "<<b<<endl;//打印出指针变量的值
 }
 	return ;
 }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!