zoukankan      html  css  js  c++  java
  • static_cast、const_cast、dynamic_cast、reinterpret_cast

    方式使用场景
    static_cast 基本数据类型之间的转换使用,例如float转int,int转char等;子类对象指针转换成父类对象指针也可以使用static_cast;在有类型指针和void*之间转换使用,不能使用static_cast在有类型指针之间进行类型转换。
    dynamic_cast 用于将父类的指针或引用转换为子类的指针或引用,此场景下父类必须要有虚函数(只要拥有虚函数就行)
    const_cast 用于常量指针或引用与非常量指针或引用之间的转换。
    reinterpret_cast 类似C语言中的强制类型转换,什么都可以转,尽量不要使用此方式。

    static_cast

    基本数据类型之间的转换使用,例如float转int,int转char等,在有类型指针和void*之间转换使用,子类对象指针转换成父类对象指针也可以使用static_cast(不能使用static_cast在有类型指针之间进行类型转换)。

     1 #include <iostream>
     2  
     3 using namespace std;
     4  
     5 struct Base {
     6     virtual void Func() { cout << "Base Func 
    "; }
     7 };
     8  
     9 struct Derive : public Base {
    10     void Func() override { cout << "Derive Func 
    "; }
    11 };
    12  
    13 int main() 
    14 {
    15     float f = 1.23;
    16     cout << "f " << f << endl;
    17     int i = static_cast<int>(f);
    18     cout << "i " << i << endl;
    19  
    20     void *p;
    21     int *i_p = static_cast<int *>(p);
    22     void *pi = static_cast<void *>(&f);
    23     int *pi = static_cast<int *>(&f);  // error invalid static_cast from type ‘float*’ to type ‘int*’
    24  
    25     Derive d;
    26     d.Func();
    27     Base *b = static_cast<Base *>(&d); 
    28     b->Func();
    29     return 0;
    30 }

    dynamic_cast

    用于将父类的指针或引用转换为子类的指针或引用,此场景下父类必须要有虚函数(只要拥有虚函数就行),因为dynamic_cast是运行时检查,检查需要运行时信息RTTI。

     1 #include <iostream>
     2  
     3 using namespace std;
     4  
     5 struct Base {
     6     virtual void Func() { cout << "Base Func 
    "; }
     7 };
     8  
     9 struct Derive : public Base {
    10     void Func() override { cout << "Derive Func 
    "; }
    11 };
    12  
    13 int main() {
    14     Derive d;
    15     d.Func();
    16     Base *b = dynamic_cast<Base *>(&d);
    17     b->Func();
    18     Derive *dd = dynamic_cast<Derive *>(b);
    19     dd->Func();
    20     return 0;
    21 }

    const_cast

    用于常量指针或引用与非常量指针或引用之间的转换,只有const_cast才可以对常量进行操作,一般都是用它来去除常量性(去除常量性是危险操作,还是要谨慎操作)。

    1 int main() {
    2     int data = 10;
    3     const int *cpi = &data;
    4  
    5     int *pi = const_cast<int *>(cpi);
    6  
    7     const int *cpii = const_cast<const int *>(pi);
    8     return 0;
    9 }

    reinterpret_cast

    类似C语言中的强制类型转换,什么都可以转,万不得已不要使用,一般前三种转换方式不能解决问题了使用这种强制类型转换方式。

    1 int main() {
    2     int data = 10;
    3     int *pi = &data;
    4  
    5     float *fpi = reinterpret_cast<float *>(pi);
    6  
    7     return 0;
    8 }
  • 相关阅读:
    Flink 状态生命周期
    jpa使用@CollectionTable创建表
    Java的四种引用---强软弱虚
    ThreadLocal与内存泄露
    Flink 1.11 Table API 实现kafka到mysql
    FLIink 1.11 SQL 构建一个端到端的流式应用
    Flink1.11编译
    Flink运行yarn-session报错 java.lang.NoClassDefFoundError: org/apache/hadoop/yarn/exceptions/YarnException
    欢迎订阅AI科技导读微信公众号,获取人工智能的最新技术解读教程!
    深度学习深刻理解和应用--人工智能从业人员必看知识
  • 原文地址:https://www.cnblogs.com/wangchaoguo-li/p/14210679.html
Copyright © 2011-2022 走看看