• C++中的类型转换


    C++中有四种类型转换,分别是:static_cast、dynamic_cast、reintepret_cast、const_cast。

    以下是实现4种类型转换的实例:

      1 #include<iostream>
      2 using namespace std;
      3 
      4 
      5 //为dynamic_cast搭建的场景
      6 class Tree{};
      7 
      8 class Animal
      9 {
     10 public:
     11     virtual void cry()=0;
     12 };
     13 
     14 class Dog:public Animal
     15 {
     16 public:
     17     void cry()
     18     {
     19         cout<<"汪汪汪"<<endl;
     20     }
     21 
     22     void doHome()
     23     {
     24         cout<<"看家"<<endl;
     25     }
     26 };
     27 
     28 class Cat:public Animal
     29 {
     30 public:
     31     void cry()
     32     {
     33         cout<<"喵喵喵"<<endl;
     34     }
     35 
     36     void doThing()
     37     {
     38         cout<<"抓老鼠"<<endl;
     39     }
     40 };
     41 
     42 
     43 
     44 void fun(Animal* pAnimal)
     45 {
     46     pAnimal->cry();
     47 
     48     //动态类型转换,父类转成子类
     49     Dog* pDog=dynamic_cast<Dog*>(pAnimal);//如果转换失败,则pDog=NULL
     50     if(pDog!=NULL)
     51     {
     52         pDog->doHome();
     53     }
     54 
     55     Cat* pCat=dynamic_cast<Cat*>(pAnimal);//如果转换失败,则pCat=NULL
     56     if(pCat!=NULL)
     57     {
     58         pCat->doThing();
     59     }
     60 }
     61 
     62 
     63 
     64 //为const_cast搭建的场景
     65 void printBuf(const char* p)
     66 {
     67     //char *p1=p; 不允许把const char*转成char*
     68     char *p1=const_cast<char*>(p);
     69     p1="我是指向char类型的指针";
     70     cout<<p1<<endl;
     71 }
     72 
     73 
     74 
     75 int main()
     76 {
     77     double pi=3.1415926;
     78     int pi_num1=(int)pi; //c语言,强制类型转换
     79     int pi_num2=static_cast<int>(pi); //c++,编译器会做类型检查,如果检查不通过会报错
     80     int pi_num3=pi;//c语言中隐式类型转换的地方均可使用c++中的static<>()进行替代
     81 
     82 
     83 
     84     //1.static_cast类型转换
     85     char* p1="Hello world";
     86     int* p2=NULL;
     87     //p2=p1;      c中不允许这种类型的强制转换
     88     //p2=static_cast<int*>(p1);    c++使用static_cast<>()进行类型转化时,编译器会做类型检查,这种类型的转换会报错
     89     
     90     
     91     //2.reinterpret_cast类型转换
     92     p2=reinterpret_cast<int*>(p1); //重新解释类型转换
     93     cout<<"p1:"<<p1<<endl; //输出"Hello world"
     94     cout<<"p2:"<<p2<<endl; //输出p2所指向的内存的首地址
     95 
     96     Tree t1;
     97     Animal* pBase=NULL;
     98     //pBase=static_cast<Animal*>(&t1); 不允许,会报错
     99     pBase=reinterpret_cast<Animal*>(&t1); //允许,有点强制类型转换的味道
    100 
    101     //总结:通过static_cast和reinterpret_cast基本上把c语言的强制类型转换都覆盖了
    102 
    103 
    104 
    105     //3.dynamic_cast类型转换,适用于父类和子类指针之间的相互转换
    106     Dog d1;
    107     Cat c1;
    108     fun(&d1);
    109     fun(&c1);
    110 
    111 
    112     //4.const_cast类型转换,把const类型转成非const类型
    113     char pc[]="我是指向const char类型的指针";//这里程序员必须保证pc指向的内存空间是可读的
    114     printBuf(pc);
    115 
    116 
    117     return 0;
    118 }
  • 相关阅读:
    美工代码注意事项(html+div+css+js)
    vss搭建于操作
    那些年用过的好工具
    javascript 和jqurry详解
    jeecg list页面鼠标悬停展示内容
    页面自适应优化备份
    查询数据字典备份
    页面 微操作备份
    小数保留位数
    从阿里云那文件 解析xml备份
  • 原文地址:https://www.cnblogs.com/jswu-ustc/p/8544406.html
Copyright © 2020-2023  润新知