zoukankan      html  css  js  c++  java
  • C++新型强制类型转换。

      C++强制类型转换分为4个不同的类型。

      1、static_cast

        -用作基本类型转换

        -不能用于基本类型指针转换。

        -可以用于有继承关系对象之间的转换和类指针之间的转换。

        

    #include <stdio.h>
    
    void static_cast_demo(void)
    {
        int i = 0x12345;
        char c = 'c';
        int* pi = &i;  //pi -> i
        char* pc = &c; //pc -> c
        
        c = static_cast<char>(i);//int i change to char c
        pc = static_cast<char*>(pi);//想通过 static_cast 将int 型指针转换为 char型指针。由于static_cast不能用于基本类型指针转换error
    }
    
    
    
    int main(int argc, char *argv[])
    {
        
        
        return 0;
    }

    编译结果:  

    test.cpp: In function ‘void static_cast_demo()’:
    test.cpp:12:28: error: invalid static_cast from type ‘int*’ to type ‘char*’

      2、const_cast

        -用于去除变量的只读属性。

        -强制内心转换的目标只能是指针或者引用  

    /*强制类型转换*/
    #include <stdio.h>
    
    void const_cast_demo(void)
    {
        const int& j = 1;//定义一个j引用常量,j拥有只读属性
        int& k = const_cast<int&>(j);//定义一个k,通过const_cast k去除了j所拥有的只读属性
        
        const int x = 2; //定义一个常量x   x拥有只读属性
        int& y = const_cast<int&>(x);//定义一个y 通过const_cast y去除了引用x时所拥有的只读属性,所以y是可变的
        
        int z = const_cast<int>(x);//想通过const_cast 来将x转换为一个int型变量,这不允许,因为const_cast 强制转换的目标只能是指针或者引用。error
        
        k = 5;
        
        printf("k = %d
    ", k);
        printf("j = %d
    ", j);
        
        y = 8;
        
        printf("x = %d
    ", x);
        printf("y = %d
    ", y);
        printf("&x = %p
    ", &x);
        printf("&y = %p
    ", &y);
    
    }
    
    
    int main(int argc, char *argv[])
    {
        
        const_cast_demo();
        return 0;
    }

    编译结果:  

    test.cpp: In function ‘void const_cast_demo()’:
    test.cpp:12:30: error: invalid use of const_cast with type ‘int’, which is not a pointer, reference, nor a pointer-to-data-member type

      3、dynamic_cast

        -用于指针类型之间的强制类型转换。

        -用于整数和指针类型之间的强制类型转换。

    #include <stdio.h>
    
    void dynamic_cast_demo()
    {
        int i = 0;
        int* pi = &i;
        char* pc = dynamic_cast<char*>(pi);//dynamic_cast 只能用于整数与指针类型之间的相互转换,这里将 整数类型转换为 char*类型 error
    }
    int main(int argc, char *argv[])
    {
        
        
        return 0;
    }

    编译结果:  

    test.cpp: In function ‘void dynamic_cast_demo()’:
    test.cpp:8:38: error: cannot dynamic_cast ‘pi’ (of type ‘int*’) to type ‘char*’ (target is not pointer or reference to class)

      4、reinterpret_cast

        -用于有继承关系指针之间的转换。

        -用于有交叉关系指针之间的转换。

        -具有类型检查功能

        -需要虚函数的支持。    

    #include <stdio.h>
    
    void reinterpret_cast_demo()
    {
        int i = 0;
        char c = 'c';
        int* pi = &i;
        char* pc = &c;
        
        pc = reinterpret_cast<char*>(pi);//将int* 类型指针 转换为 char*
        pi = reinterpret_cast<int*>(pc);//将char* 类型指针 转换为 int*
        pi = reinterpret_cast<int*>(i);//将int 类型转换为 int*类型指针
        c = reinterpret_cast<char>(i); //想 通过reinterpret_cast  将int型 转换为char型 普通类型转换使用static_cast error 
    }
    int main(int argc, char *argv[])
    {
        
        
        return 0;
    }

       编译结果:   

    test.cpp: In function ‘void reinterpret_cast_demo()’:
    test.cpp:14:33: error: invalid cast from type ‘int’ to type ‘char

      

    用法格式:xxx_cast <type>(Expression)

      

  • 相关阅读:
    UITableView的简单使用
    Oracle相关博客转移
    使用Outlook 2010,拖拽大于20M附件发生“附件大小超过了允许的范围”提示的解决方法
    Use OpenLDAP as security provider in Oracle UCM 11g
    PostBuild event不能注册到全局缓存解决办法
    WIN7下丢失的光驱解决办法:由于其配置信息不完整或已损坏,Windows 无法启动这个硬件设备
    我与计算机的故事
    nova http 409 虚拟机状态重置
    百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换(JS版代码)
    未能从程序集加载类型
  • 原文地址:https://www.cnblogs.com/hjxzjp/p/11630154.html
Copyright © 2011-2022 走看看