我是靠谱客的博主 虚心猫咪,最近开发中收集的这篇文章主要介绍C++类型转换cast,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

C++类型转换

static_cast
注意向下转换时(不可以虚拟继承)
static_cast can also cast through inheritance hierarchies. It is unnecessary when casting upwards (towards a base class), but when casting downwards it can be used as long as it doesn’t cast through virtual inheritance. It does not do checking, however, and it is undefined behavior to static_cast down a hierarchy to a type that isn’t actually the type of the object.
const_cast
将const转换为变量
const_cast can be used to remove or add const to a variable
重载时很有用
This can be useful when overloading member functions based on const, for instance. It can also be used to add const to an object, such as to call a member function overload.
dynamic_cast
仅用于处理多态,不光可以向下,还可以同继承等级,或者向上转换;如果转换失败返回nullptr(指针转换),或者抛出std::bad_cast(引用转换)
dynamic_cast is exclusively used for handling polymorphism. You can cast a pointer or reference to any polymorphic type to any other class type (a polymorphic type has at least one virtual function, declared or inherited). You can use it for more than just casting downwards – you can cast sideways or even up another chain. The dynamic_cast will seek out the desired object and return it if possible. If it can’t, it will return nullptr in the case of a pointer, or throw std::bad_cast in the case of a reference.
局限性:不适用于没有使用虚拟继承的同类对象转换;
dynamic_cast has some limitations, though. It doesn’t work if there are multiple objects of the same type in the inheritance hierarchy (the so-called ‘dreaded diamond’) and you aren’t using virtual inheritance. It also can only go through public inheritance - it will always fail to travel through protected or private inheritance. This is rarely an issue, however, as such forms of inheritance are rare.
reinterpret_cast
常用于数据类型转换,如将未处理的数据转换为数据,或存储数据
reinterpret_cast is the most dangerous cast, and should be used very sparingly. It turns one type directly into another — such as casting the value from one pointer to another, or storing a pointer in an int, or all sorts of other nasty things. Largely, the only guarantee you get with reinterpret_cast is that normally if you cast the result back to the original type, you will get the exact same value (but not if the intermediate type is smaller than the original type). There are a number of conversions that reinterpret_cast cannot do, too. It’s used primarily for particularly weird conversions and bit manipulations, like turning a raw data stream into actual data, or storing data in the low bits of a pointer to aligned data.
总结:
Use dynamic_cast for converting pointers/references within an inheritance hierarchy.
dynamic_cast用于转换继承指针/引用

Use static_cast for ordinary type conversions.
static_cast用于普通类型转换

Use reinterpret_cast for low-level reinterpreting of bit patterns. Use with extreme caution.
reinterpret_cast用于低级别的位转换,使用时小心,注意类型大小的差异

Use const_cast for casting away const/volatile. Avoid this unless you are stuck using a const-incorrect API.
const_cast用于常数/volatile转换

最后

以上就是虚心猫咪为你收集整理的C++类型转换cast的全部内容,希望文章能够帮你解决C++类型转换cast所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部