• c++中的类型转换


    c++风格的类型转换提供了4种类型转换操作符来进行不同场合的应用。

      static_cast      静态类型转换。如int转换成char

      reinterpreter_cast   重新解释类型

      dynamic_cast       命令上理解是类型转换。如父类和子类之间的多类型转换

      const_cast      字面上的理解就是去const属性

    4种类型转换的格式:

       TYPE B = static_cast<TYPE>(a)

    前两种类型转换:

    #include<iostream>
    using namespace std;
    
    
    int main()
    {
    
        double dpi = 3.1415926;
    
        int num1 = (int)dpi;//c类型转换
        int num2 = static_cast<int>(dpi);//静态类型转换,c++编译时会做类型检查
        int num3 = dpi;//c语言中,隐式类型转换均可以使用static_cast<>()进行类型转换
    
        ////char* => int*
        char ch1[12] = "hellow";
        char *p1 = ch1;
        int* p2 = NULL;
    
        //p2 = p1;
        //p2 = static_castL<int*>(p1)//使用static_cast,编译器会做类型检查,若有错误提示错误
    
        p2 = reinterpret_cast<int*>(p1);//类型不同,reinterpret_cast相当于强制类型转换
        cout << "p1: " << p1 << endl; //%s
        cout << "p2: "<< p2 << endl;  //%d
    
        //总结:static_cast,reinterpret_cast相当于把所有c语言的强制类型转换实现了
        system("pause");
        return 0;
    }

    reinterpret_cast类型转换:

    #include<iostream>
    using namespace std;
    
    
    class tree
    {
    
    };
    class Animal
    {
    public:
        virtual void cry() = 0;
    };
    class dog :public Animal
    {
    public:
        virtual void cry()
        {
            cout << "wangwang" << endl;
        }
        void doHome()
        {
            cout << "看家" << endl;
        }
    };
    class cat :public Animal
    {
    public:
    
        virtual void cry()
        {
            cout << "miaomiao" << endl;
        }
        void doHome()
        {
            cout << "抓老鼠" << endl;
        }
    };
    
    void playObj(Animal* base)
    {
        base->cry();//1、有继承 2、虚函数重写 3、父类指针,只想子类对象==>多态
        //需求:能识别子类对象
        //dynamic_cast运行时类型识别 RIIT
        dog* pDog = dynamic_cast<dog*>(base);//父类对象==>子类对象,向下转型
                                             //把父亲转化成儿子
        if (pDog != NULL)
        {
            pDog->doHome();//让做自己特有的工作
        }
        cat* pCat = dynamic_cast<cat*>(base);
        if (pCat != NULL)
        {
            pCat->doHome();//让做自己特有的工作
        }
    }
    int main()
    {
    
        dog d1;
        cat c1;
    
        Animal *pBase = NULL;
        pBase = &d1;
        pBase = static_cast<Animal*>(&d1);//让c++编译器在编译的时候进行类型检查
    
        //强制类型转换
        pBase = reinterpret_cast<Animal*>(&d1);
        {
            tree t1;
            //pBase = static_cast<Animal*>(&t1);//c++编译器提前检查不通过
            pBase = reinterpret_cast<Animal*>(&t1);//reinterpret_cast重新解释,强行转换的味道
        }
    
    
        playObj(&d1);
        playObj(&c1);
        system("pause");
        return 0;
    }

    const_cast类型转换:

    #include<iostream>
    using namespace std;
    
    
    void printBuf(const char* p);
    int main()
    {
        char buf[] = "aaaaaaaaaaaffffffffff";
        //程序员要确保p所指向的内存空间确实能修改,如果不能修改将产生灾难性后果
        printBuf(buf);
        system("pause");
        return 0;
    }
    //const char *p 的const 修饰 让p指向的内存空间变成只读属性
    void printBuf(const char* p)
    {
        //p[0] = 'Z';//会报错 不能给只读的内存空间赋值
    
        char *p1 = NULL;
        p1 = const_cast<char *>(p);//转换之前变量是只读类型的  转换之后变量是可以改写的类型
        //const char * ==>   char *
        p1[0] = 'Z';//通过p1去修改了内存空间
        cout << p << endl;
    }

     

  • 相关阅读:
    js根据ip地址获取城市地理位置
    vue-cli项目中使用mock结合axios-mock-adapter生成模拟数据
    将图片转canvas
    判断浏览器是否联网
    头尾固定中间高度自适应布局 css
    经典闭包
    多余文字转化为省略号css
    $.grep()
    node 图片转base64
    CentOS 7下安装Mysql 5.7
  • 原文地址:https://www.cnblogs.com/ymj11/p/13783112.html
Copyright © 2020-2023  润新知