C++ 4种Cast

C++ 4种CastC 语言的基本类型转换 字节截断 补充需要的字节 直接利用编译器 查看内存后 可以自己计算值是什么 一般的 int char float double 等等 cplusplus 英文解释 http www cplusplus com doc tutorial typecasting 参考博文 http blog chinaunix

大家好,我是讯享网,很高兴认识大家。

C语言的基本类型转换

  • 字节截断
  • 补充需要的字节

static_cast

static_cast can perform conversions between pointers to related classes, not only upcasts (from pointer-to-derived to pointer-to-base), but also downcasts (from pointer-to-base to pointer-to-derived). No checks are performed during runtime to guarantee that the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other side, it does not incur the overhead of the type-safety checks of dynamic_cast.

用于类层次结构中基类和派生类之间指针或引用的转换

  • 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的
  • 进行下行转换(把基类的指针或引用转换为派生类表示),由于没有动态类型检查,所以是不安全的
 static_cast is also able to perform all conversions allowed implicitly (not only those with pointers to classes), and is also able to perform the opposite of these. It can: Convert from void* to any pointer type. In this case, it guarantees that if the void* value was obtained by converting from that same pointer type, the resulting pointer value is the same. Convert integers, floating-point values and enum types to enum types. 

讯享网
  • void*, 任何类型的指针 转换
  • 基本类型转换。 如把int转换成char,这种转换的安全也要开发人员来保证


    讯享网

注意:static_cast不能转换掉expression的const、volitale或者__unaligned属性。

dynamic_cast

dynamic_cast can only be used with pointers and references to classes (or with void*). Its purpose is to ensure that the result of the type conversion points to a valid complete object of the destination pointer type.

This naturally includes pointer upcast (converting from pointer-to-derived to pointer-to-base), in the same way as allowed as an implicit conversion.

But dynamic_cast can also downcast (convert from pointer-to-base to pointer-to-derived) polymorphic classes (those with virtual members) if -and only if- the pointed object is a valid complete object of the target type.

dynamic_cast主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。

在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。

讯享网class Base { virtual void dummy() {} }; class Derived: public Base { int a; }; int main () { try { Base * pba = new Derived; // 基类指针,但实际类型是 派生类 Base * pbb = new Base; // 真正的基类指针 Derived * pd; pd = dynamic_cast<Derived*>(pba); // 安全的类型转换 if (pd==0) cout << "Null pointer on first type-cast.\n"; pd = dynamic_cast<Derived*>(pbb); // 不完整的类型,无效 if (pd==0) cout << "Null pointer on second type-cast.\n"; } catch (exception& e) { 
  
    
  cout << "Exception: " << e.what();} return 0; }

上面的程序注意点

  • Base类,必须有virtual函数, static_cast没有这个限制

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 (such as intptr_t), is guaranteed to be able to be cast back to a valid pointer.

The conversions that can be performed by reinterpret_cast but not by static_cast are low-level operations based on reinterpreting the binary representations of the types, which on most cases results in code which is system-specific, and thus non-portable. For example:

reinterpret_cast 任何类型的指针转换,操作结果只是简单的从一个指针到别的指针的值的二进制拷贝。在类型之间指向的内容不做任何类型的检查和转换。

int n=9; int *pt = &n; pt = reinterpret_cast<int *> (n); // pt = 0x00000009
讯享网// 不安全的转换 class A { /* ... */ }; class B { /* ... */ }; int main () { A * a = new A; B * b = reinterpret_cast<B*>(a); return 0; }

const_cast

This type of casting manipulates the constness of the object pointed by a pointer, either to be set or to be removed. For example, in order to pass a const pointer to a function that expects a non-const argument:

void print (char * str) { cout << str << '\n'; } int main () { const char * c = "sample text"; // print(c); print ( const_cast<char *> (c) ); return 0; }
小讯
上一篇 2025-02-21 15:09
下一篇 2025-01-10 16:37

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/65330.html