C++ static_cast dynamic_cast reinterpret_cast const_cast转换
static_cast <type-id> ( expression )
和C风格的类型转换相似,可以转换一个指针到基类,或者派生类。不做Run-time类型检查,这样转换并不总是安全的。也可以转换枚举类型到int或者int到float。使用static_cast从基类指针转换成派生类指针可能不安全,因为派生类可能有基类没有的函数或者变量。但是从派生类指针转换成基类指针,通常是安全的。
reinterpret_cast < type-id > ( expression )
允许指针类型转换成其他指针类型,并允许整型转换为任务指针类型反之亦然。不对类型转换做校验。reinterpret_cast会将null指针值转换为目标类型的null指针值。reinterpret_cast是非常不安全的,尽量避免使用。
const_cast < type-id > ( expression )
用于从类中移除const、 volatile和 __unaligned等属性。对于指针和引用将引用原始对象,对于指向的数据成员将引用原始的数据成员。不能使用const_cast覆盖常量的常量状态。
dynamic_cast < type-id > ( expression )
只用于指针和引用,有Run-time检查。type-id必须为一个已经定义的类的指针或者引用或者使用void*。如果type-id为指针,则表达式必须为指针。
bad_cast
class bad_cast : public exception {
public:
bad_cast(const char * _Message = "bad cast");
bad_cast(const bad_cast &);
virtual ~bad_cast();
};
当使用dynamic_cast转换失败时,会抛出bad_cast异常。
总结:
对于指针和引用的转换使用dynamic_cast,对于普通整型值转换使用static_cast,移除const等关键字时用const_cast,但是实际上并不能修改常量的值。尽量避免使用reinterpret_cast。
C++ static_cast dynamic_cast reinterpret_cast const_cast转换的更多相关文章
- c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast
c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast [版权声明]转载请注明出处 http://www.cnblogs.c ...
- C++雾中风景11:厘清C++类型转换(static_cast,dynamic_cast,reinterpret_cast,const_cast)
C++是一门弱类型的语言,提供了许多复杂和灵巧类型转换的方式.笔者之前写的Python与Go都是强类型的语言,对这种弱类型的设计实在是接受无力啊~~ ( 生活所迫,工作还得写C++啊~~)C++语言提 ...
- static_cast, dynamic_cast, reinterpret_cast, const_cast区别比较
隐式转换(implicit conversion) ; int b; b=a; short是两字节,int是四字节,由short型转成int型是宽化转换(bit位数增多),编译器没有warning,如 ...
- static_cast, dynamic_cast, reinterpret_cast, const_cast的区别
static_cast最像C风格的强制转换,很多时候都需要程序员自身去判断转换是否安全.但是相对C风格的强制转换,在无关类的类指针之间转换上,有安全性的提升. dynamic_cast是运行时的转换吧 ...
- reinterpret_cast,static_cast, dynamic_cast,const_cast的运用分析
reinterpret_cast(重新解释类型转换) reinterpret_cast 最famous的特性就是什么都可以,转换任意的类型,包括C++所有通用类型,所以也最不安全 应用 整形和指针之间 ...
- 你也许还不知道const_cast,static_cast,dynamic_cast,reinterpret_cast的区别吧?
[QQ群: 189191838,对算法和C++感兴趣可以进来] 开篇立意: C++中各种转换令人眼花缭乱,看似差不多,实际差很多,而且在当今时间,做一个"差不多先生"其 ...
- C++中四种类型转换方式(ynamic_cast,const_cast,static_cast,reinterpret_cast)
Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变一个变量的类型为别的类型从而 ...
- C++提供的四种新式转换--const_cast dynamic_cast reinterpret_cast static_cast
关于强制类型转换的问题,许多书都讨论过,写的最具体的是C++之父的<C++的设计和演化>. 最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_c ...
- c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast
c++强制类型转换:dynamic_cast.const_cast .static_cast.reinterpret_cast 博客分类: C/C++ CC++C#编程数据结构 dynamic_ca ...
随机推荐
- SVN弱密码扫描(Python)
寂寞如雪的用脑过度,所以来写个博客分享一下.#虽然上一篇博客我还没写完 SVN的弱密码,看起来很复杂,但实际上很简单啊= =虽然不像pymssql/mymssql这种,Python提供了很好用的包,但 ...
- iOS 的 Gif 渲染引擎 FLAnimatedImage-b
公司的项目有个首页加载一张2M左右的git图,刚做的时候是使用的SDWebImage里面的方法: + (UIImage *)sd_animatedGIFNamed:(NSString *)name; ...
- MyISAM读写并发优化
MyISAM在读操作占主导的情况下是很高效的.可一旦出现大量的读写并发,同InnoDB相比,MyISAM的效率就会直线下降,而 且,MyISAM和InnoDB的数据存储方式也有显著不同:通常,在MyI ...
- IntelliJ IDEA 进行js Debug调试
idea的js调试目前看来不同给力,一是玩转它需要安装谷歌插件支持,二是貌似存在一些bug... 一.新建一个jsp并打上断点 二.调试 idea出现提示: 安装JetBrains IDE Suppo ...
- js中批量处理样式——cssText的使用
http://www.cnblogs.com/snandy/archive/2011/03/12/1980444.html
- Apache URL rewrite 配置
下面是Apache的配置过程,可以参考下:1.httpd.conf配置文件中加载了mod_rewrite.so模块,使用虚拟主机 #LoadModule rewrite_module modules/ ...
- JSP中脚本、声明和表达式的本质区别
JSP脚本元素 使用JSP脚本元素可以将Java代码嵌入到JSP页面里,这些Java代码将出现在由当前JSP页面生成的Servlet中,使JSP将静态内容与动态内容分离出来.脚本元素包含: 1. ...
- Howto: Deploy VC2008 apps without installing vcredist_x86.exe
There are several reasons for xcopy deployment of an application (also known as application local). ...
- nginx配置静态文件服务器
搭建文件服务器 要点就是root目录,会自动指向索引文件 如: index, index.html等 server { client_max_body_size 4G; listen 80; ## l ...
- Oracle程序包
程序包由两部分构成:规范(specification)和主体(body). 创建表 create table PEOPLE ( ID NUMBER primary key not null, NAME ...