C++各类型转换的总结
来源:互联网 发布:js中on和bind的区别 编辑:程序博客网 时间:2024/06/14 16:49
dynamic_cast
dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.
When a class is polymorphic, dynamic_cast performs a special checking during runtime to ensure that the expression yields a valid complete object of the requested class:
If dynamic_cast is used to convert to a reference type and the conversion is not possible, an exception of typebad_cast is thrown instead.
dynamic_cast can also cast null pointers even between pointers to unrelated classes, and can also cast pointers of any type to void pointers (void*).
static_cast
static_cast can perform conversions between pointers to related classes, not only from the derived class to its base, but also from a base class to its derived. This ensures that at least the classes are compatible if the proper object is converted, but no safety check is performed during runtime to check if the object being converted is in fact a full object of the destination type.
static_cast can also be used to perform any other non-pointer conversion that could also be performed implicitly
reinterpret_cast
reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other. All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.
It can also cast pointers to or from integer types. The format in which this integer value represents a pointer is platform-specific. The only guarantee is that a pointer cast to an integer type large enough to fully contain it, is granted to be able to be cast back to a valid pointer.
const_cast
This type of casting manipulates the constness of an object, either to be set or to be removed
总结:dynamic_cast 可以由派生类转换到基类,也可由指向派生类的基类转换成派生类。
static_cast 可以在派生类和基类中自由转换。
reinterpret_cast类可以在任何类中自由转换。
以上转换都是指针间的转换
static_cast 还可用于非指针的各基本数据类型间的转换。
reinterpret_cast可用于任何指针间的转换,也可用于从整数类型到指针间的转换。
- C++各类型转换的总结
- json的各类转换总结
- c语言各类型转换
- java 各类型之间转换总结
- PHP中各类型数据转换为bool型时为false的总结
- Javascript各类型值之间的转换
- C#类型转换总结(三)各类型转化实例
- python 各类型转换
- 各类型变量的自动初始化总结
- Bundle 保存各类型数据的总结
- C中各类型的长度
- C 的各类细节
- Python各类数据类型转换函数总结
- Java各类型间转换
- NSNumber各类型包装转换
- java中字节数组与其他各类型的转换
- 经典的各类排序总结
- 各类排序算法的总结
- 《程序员》-移动时代的经济学——碎片化应用
- C extern
- arcengine 以数据库方式读取dbf表
- iphone相册源码下载(仿three20)
- 动态换肤
- C++各类型转换的总结
- 用C语言输出自身的程序源码(Quine)
- Java编码规范之声明和空白,空格
- AS3 Flex基础知识100条
- 使用Geoprocessor 计算面积和长度
- android-查看关键字是否存在的文件
- Flex复制内容到剪切板
- Qt学习——聊天的QQ列表QToolBox类 .
- 第七章 类型转换:深入浅出struts2