C++类型转化:static_cast,reinterpret_cast,dynamic_cast,const_cast
类型转换名称和语法
C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:
TYPE b = (TYPE)a
C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。
static_cast 静态类型转换。如int转换成char
reinterpreter_cast 重新解释类型
dynamic_cast 命名上理解是动态类型转换。如子类和父类之间的多态类型转换。
const_cast, 字面上理解就是去const属性。
4种类型转换的格式:
TYPE B = static_cast<TYPE> (a)
demo 1
#include <iostream> #include <cstdio> using namespace std; int main() { double pi = 3.1415926; int num1 = (int)pi; // C类型转换 int num2 = static_cast<int>(pi); // 静态类型转换,编译的时候C++编译器会做类型检查 int num3 = pi; // C语言中隐式类型转换的地方,均可使用static_cast<>()进行类型转换 char *p1 = "hello"; int *p2 = NULL; //p2 = static_cast<int *>(pi); // 使用static_cast,编译时编译器会报错 // 会做类型检查,若有错误,提示错误 p2 = reinterpret_cast<int *>(p1); // 若不同类型之间,进行强制类型转换,用reinterpret_cast<>()进行重新解释 cout << "p1: " << p1 << endl; // %s cout << "p2: " << p2 << endl; // %d /* p1: hello p2: 0127CA54 */ return 0; }
总结:
1)static_cast<>() 静态类型转换,编译的时c++编译器会做类型检查;
基本类型能转换 但是不能转换指针类型。
2)若不同类型之间,进行强制类型转换,用reinterpret_cast<>() 进行重新解释;
3)一般性结论:
C语言中 能隐式类型转换的,在c++中可用 static_cast<>()进行类型转换。因C++编译器在编译检查一般都能通过;
C语言中不能隐式类型转换的,在c++中可以用 reinterpret_cast<>() 进行强行类型。
解释。总结:static_cast<>()和reinterpret_cast<>() 基本上把C语言中的 强制类型转换给覆盖。
reinterpret_cast<>()很难保证移植性。
demo 2 - dynamic_cast<>(),动态类型转换,安全的基类和子类之间转换;运行时类型检查
#include <iostream> #include <cstdio> using namespace std; class Tree {}; class Animal { virtual void cry() = 0; }; class Dog : public Animal { public: virtual void cry() { cout << "bowwow~" << endl; } void doHome() { cout << "look after the house!" << endl; } }; class Cat : public Animal { public: virtual void cry() { cout << "meow" << endl; } void doThing() { cout << "catch a Mouse" << endl; } }; void playObj(Animal *base) { base->cry(); // 1有继承,2虚函数重载,3父类指针,指向子类对象。 // 会发生多态 // 识别子类对象 // dynamic_cast<>()运行时类型识别 Dog *pDog = dynamic_cast<Dog *>(base); if (pDog != NULL) { pDog->doHome(); } Cat *pCat = dynamic_cast<Cat *>(base); if (pCat != NULL) { pCat->doThing(); } } int main() { Dog d1; Cat c1; Animal *pBase = NULL; pBase = &d1; pBase = static_cast<Animal *>(&d1); // 让C++编译器在编译的时候进行类型检查 // 强制类型转换 pBase = reinterpret_cast<Animal *>(&d1); { Tree t1; //pBase = static_cast<Animal *>(&t1); // C++编译器做类型检查,报错 pBase = reinterpret_cast<Animal *>(&t1); // reinterpret_cast重新解释,这里有强制类型转换的感觉 } playObj(&d1); playObj(&c1); return 0; }
demo 3 - const_cast<>(),去除变量的只读属性
#include <iostream> #include <cstdio> using namespace std; // const char *p的const修饰,让p指向的内存空间变成只读属性 void printBuf(const char *p) { //p[0] = 'Z'; // 报错 char *p1 = NULL; // 程序员需要清楚的知道变量转换之前是什么类型,转换之后是什么类型 // const char * ==> char * 把只读属性去掉了 p1 = const_cast<char *>(p); p1[0] = 'Z'; // 通过p1修改了内存空间 cout << p << endl; } int main() { char buf[] = "aaaaaaaasssssssssdddddddd"; printBuf(buf); // 注意,要确保p所指向的内存空间确实能修改,如果不能修改,会带来灾难性后果 char *myp = "aaaaaadfdafaf"; // myp指向一块常量空间,这里是无论如何都不能被修改的 //printBuf(myp); // 运行直接奔溃 return 0; }
总结
结论1:程序员要清除的知道: 要转的变量,类型转换前是什么类型,类型转换后是什么类型。转换后有什么后果。
结论2:一般情况下,不建议进行类型转换;避免进行类型转换。
C++类型转化:static_cast,reinterpret_cast,dynamic_cast,const_cast的更多相关文章
- static_cast, dynamic_cast, const_cast 三种类型转化的区别
强制转化四种类型可能很多人都常常忽略就象我一样,但是有时还是比较有用的.不了解的建议看看,一些机制我也不是十分了解,只是将一些用法写出来让大家看看. ...
- C++强制类型转换:static_cast、dynamic_cast、const_cast、reinterpret_cast
1. c强制转换与c++强制转换 c语言强制类型转换主要用于基础的数据类型间的转换,语法为: (type-id)expression//转换格式1 type-id(expression)//转换格式2 ...
- C++的类型转换:static_cast、dynamic_cast、reinterpret_cast和const_cast
在C++中,存在类型转换,通常意味着存在缺陷(并非绝对).所以,对于类型转换,有如下几个原则:(1)尽量避免类型转换,包括隐式的类型转换(2)如果需要类型转换,尽量使用显式的类型转换,在编译期间转换( ...
- static_cast、dynamic_cast、reinterpret_cast、和const_cast
关于强制类型转换的问题,很多书都讨论过,写的最详细的是C++ 之父的<C++ 的设计和演化>.最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_ ...
- C++ ------ static_cast,dynamic_cast,reinterpret_cast,const_cast
C++类型转换分为:隐式类型转换和显式类型转换 第1部分. 隐式类型转换 又称为“标准转换”,包括以下几种情况:1) 算术转换(Arithmetic conversion) : 在混合类型的算术表达式 ...
- C++里的强制类型转换符reinterpret_cast、static_cast 、dynamic_cast、const_cast 区别
C 风格(C-style)强制转型如下: (T) exdivssion // cast exdivssion to be of type T 函数风格(Function-style)强制转型使用这样的 ...
- C++的类型转换:static_cast、dynamic_cast、reinterpret_cast和const_cast(dynamic_cast还支持交叉转换,const_cast将一个类的const、volatile以及__unaligned属性去掉)
在C++中,存在类型转换,通常意味着存在缺陷(并非绝对).所以,对于类型转换,有如下几个原则:(1)尽量避免类型转换,包括隐式的类型转换(2)如果需要类型转换,尽量使用显式的类型转换,在编译期间转换( ...
- static_cast、dynamic_cast、reinterpret_cast、const_cast以及C强制类型转换的区别
static_cast 1. 基础类型之间互转.如:float转成int.int转成unsigned int等 2. 指针与void*之间互转.如:float*转成void*.CBase*转成void ...
- static_cast dynamic_cast const_cast reinterpret_cast总结对比
[本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...
随机推荐
- Go 语言基础语法
Go 标记 Go 程序可以由多个标记组成,可以是关键字,标识符,常量,字符串,符号.如以下 GO 语句由 6 个标记组成: fmt.Println("Hello, World!") ...
- PHP Switch 语句
PHP Switch 语句 switch 语句用于根据多个不同条件执行不同动作. PHP Switch 语句 如果您希望有选择地执行若干代码块之一,请使用 switch 语句. 语法 switch ( ...
- [LaTeX] box algorithm adjustbox subfigures, textwidth
algorithm \begin{figure}[!t] \vspace*{\dimexpr\fboxsep+\fboxrule} \fbox{% \parbox{\dimexpr\linewidth ...
- [Angular2]eclipse中angular2开发环境的搭建
本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan 环境准备 1.eclipse neon 2.网络连接 插件地址 eclipse的插件市场地址: htt ...
- LAB颜色空间各通道的取值范围
简介 LAB颜色空间在计算机视觉中经常被使用,知道L,A,B三个通道的取值范围有一定的意义. OpenCV获取LAB取值范围 下面是一段实验代码,用于获取LAB的取值范围. 基本思路是,排列组合所有R ...
- JVM远程DEBUG(JPDA )
原理 1. JPDA简介 JPDA(Java Platform Debugger Architecture)为Java平台上的调试器定义了一个标准的体系结构.该体系结构包括3个主要组成部分:JVM T ...
- (Java)微信之个人公众账号开发(二)——接收并处理用户消息(下)
接下来,我们再讲一下图文消息: 如图: 大家可以先从开发者文档中了解一下图文消息的一些参数: 如上图,用户回复4时,ipastor返回了几条图文消息,上图中属于多图文消息,当然还有单图文消息,图文消息 ...
- 28 自定义View侧滑栏
ScrollMenuView.java package com.qf.sxy.customview03.widget; import android.content.Context; import a ...
- Xcode的playground中对于SpriteKit物理对象的更新为何无效
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 为了便于SpriteKit中物理行为的调试,我们可以借助于Xc ...
- Android文件(File)操作
Android 使用与其他平台上基于磁盘的文件系统类似的文件系统. 本文讲述如何使用 Android 文件系统通过 File API 读取和写入文件. File 对象适合按照从开始到结束的顺序不跳过地 ...