• reinterpret_cast运算符


    reinterpret_cast运算符

    reinterpret_cast运算符

    Visual Studio 2012
    此主题尚未评级 - 评价此主题

    允许所有指针转换为其他指针类型。 并允许任何整型转换为任何指针类型反之亦然。

    reinterpret_cast < type-id > ( expression )
    

    reinterpret_cast 运算符的滥用可以轻松地是不安全的。 除非所需的转换本身低级别,应使用其他转换运算符之一。

    reinterpret_cast 运算符可用于对 Unrelated_class*的转换例如 char*int*One_class* 使用,本质上是不安全的。

    reinterpret_cast 的结果不能提供任何安全性使用除转换外回其原始类型。 其他用途,时, nonportable。

    reinterpret_cast 运算符无法转换 constvolatile__unaligned 属性。 有关如何移除这些属性的信息,请参见 const_cast 运算符

    reinterpret_cast 运算符将 null 指针值为目标类型的 null 指针值。

    reinterpret_cast 的实际用于哈希函数,映射到其值设置为索引,在两个不同的值很少最终获取相同索引。

    // expre_reinterpret_cast_Operator.cpp
    // compile with: /EHsc
    #include <iostream>
    
    // Returns a hash code based on an address
    unsigned short Hash( void *p ) {
       unsigned int val = reinterpret_cast<unsigned int>( p );
       return ( unsigned short )( val ^ (val >> 16));
    }
    
    using namespace std;
    int main() {
       int a[20];
       for ( int i = 0; i < 20; i++ )
          cout << Hash( a + i ) << endl;
    }
    

    reinterpret_cast 允许指针被视为一个整型。该结果然后位从及自身的 XORed 导致唯一索引 (仅对于高度概率)。索引通过对函数的返回类型的标准 C 样式转换然后截断。

  • 相关阅读:
    oracle插入数据
    保存图片
    ASCII码排序及md5加密
    JavaScript
    HTML
    py访问Redis和zk操作
    Zookeeper集群搭建以及python操作zk
    并发编程
    Python之socket(套接字)
    Python 网络编程
  • 原文地址:https://www.cnblogs.com/lexus/p/2841449.html
Copyright © 2020-2023  润新知