zoukankan      html  css  js  c++  java
  • 十一、类型转换

    1、强制类型转换

    1.1 C方式的强制类型转换

    (Type)(Expression)
    Type (Expression)
    
    #include <stdio.h>
    
    typedef void(PF)(int);
    
    struct Point
    {
        int x;
        int y;
    };
    
    int main()
    {
        int v = 0x12345;
        PF* pf = (PF*)v;
        char c = char(v);
        Point* p = (Point*)v;
        
        pf(5);
        
        printf("p->x = %d
    ", p->x);
        printf("p->y = %d
    ", p->y);
    
        return 0;
    }
    
    

    C方式强制类型转换存在的问题

    • 过于粗暴:任意类型之间都可以进行转换,编译器很难判断其正确性
    • 难于定位:在源码中无法快速定位所有使用强制类型转换的语句

    C语言的强制类型转换已经足够简单了,如果从语法上进行改进,产生的新的语言,必然没办法兼容原来的语言,所以没办法直接改进强制类型转换,必须提出新的办法

    1.2 c++强制类型转换

    C++在设计时,进行了强制类型转换的功能上的划分,分为了4中不同的类型,产生了4个不同的关键字

    static_castcosnt_castdynamic_castreinterpret_cast

    用法:xxx_cast<Type>(Expression)

    用关键字进行强制类型转换,搜素关键字就可以将所有用到强制类型转换的地方全部搜索出来,方便定位

    • static_cast:静态类型转换

      用于基本类型间的转换

      不能用于基本类型指针间的转换

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

    • cosnt_cast

      用于去除变量的只读属性

      强制类型转换的目标类型必须是指针或引用

    • reinterpret_cast

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

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

    • dynamic_cast

      用于有继承关系的类指针间的转换

      用于有交叉关系的类指针间的转换

      具有类型检查的功能

      需要虚函数的支持

    #include <stdio.h>
    
    void static_cast_demo()
    {
        int i = 0x12345;
        char c = 'c';
        int* pi = &i;
        char* pc = &c;
        
        c = static_cast<char>(i);	// 将i强制类型转化为char
        pc = static_cast<char*>(pi);	// 整型转 char *
        							// err
    }
    
    void const_cast_demo()
    {
        const int& j = 1;	// 只读变量
        int& k = const_cast<int&>(j); // 去除了只读属性
        
        const int x = 2;	// 常量
        int& y = const_cast<int&>(x);	// 不能去除常量的只读属性,但是会重新分配一个新的空间,y是别名
        
        int z = const_cast<int>(x);		// cosnt_cast只能用于指针或引用,目标类型为int, err
        
        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);
    }
    
    void reinterpret_cast_demo()
    {
        int i = 0;
        char c = 'c';
        int* pi = &i;
        char* pc = &c;
        
        pc = reinterpret_cast<char*>(pi);
        pi = reinterpret_cast<int*>(pc);
        pi = reinterpret_cast<int*>(i);
        c = reinterpret_cast<char>(i); 	// err,
    }
    
    void dynamic_cast_demo()
    {
        int i = 0;
        int* pi = &i;
        char* pc = dynamic_cast<char*>(pi);	// 类指针之间,且要虚函数,故err
    }
    
    int main()
    {
        static_cast_demo();
        const_cast_demo();
        reinterpret_cast_demo();
        dynamic_cast_demo();
        
        return 0;
    }
    
    

    2、小结

    C方式的强制类型转换

    • 粗暴
    • 潜在的问题不容易发现
    • 不易在代码中定位

    C++关键字方式的新式类型转换

    • 编译器能够帮助检查潜在的问题
    • 非常方便地在代码中定位
    • 支持动态类型识别(dynamic_cast
  • 相关阅读:
    [轉]windows下mysql 启动 mysqlbinlog二进制日志文件
    [轉]MySQL创建、删除、重建和查看索引命令
    [轉]PHP权限控制系统PHPGACL
    [轉]mysql5存储过程语法
    Web Application Stress Tool(WAS) & SQLIOSim
    information_schema資料庫表信息
    [轉]MySQL系统变量应用探究
    [轉]httping 1.5.2 发布,HTTP连接响应测试
    [轉]批处理命令手册
    Google Native Client介紹
  • 原文地址:https://www.cnblogs.com/chenke1731/p/9626208.html
Copyright © 2011-2022 走看看