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)

      

  • 相关阅读:
    Leetcode 538. Convert BST to Greater Tree
    Leetcode 530. Minimum Absolute Difference in BST
    Leetcode 501. Find Mode in Binary Search Tree
    Leetcode 437. Path Sum III
    Leetcode 404. Sum of Left Leaves
    Leetcode 257. Binary Tree Paths
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
    Leetcode 226. Invert Binary Tree
    Leetcode 112. Path Sum
    Leetcode 111. Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/hjxzjp/p/11630154.html
Copyright © 2011-2022 走看看