码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++类型转换dynamic_cast/reinterpret_cast/static_cast/const_cast笔记


    C++类型转换dynamic_cast/reinterpret_cast/static_cast/const_cast

    C++中类型转换一般这样写

    double double_age = 13.9;
    int int_age = int(age);
    // int int_age =  (int)double_age; //也可以这样写
    // int int_age =  (int)(double_age); //也可以这样写
    // int int_age =  ((int)(double_age)); //当然也可以这样写,只要您不觉得累
    
    • 1
    • 2
    • 3
    • 4
    • 5

    但是今天讲的是强制类型转换
    https://cplusplus.com/doc/tutorial/typecasting/

    dynamic_cast (expression)
    reinterpret_cast (expression)
    static_cast (expression)
    const_cast (expression)

    dynamic_cast

    如果父类指针强制转换为子类,dynamic_cast转换判断出不安全,会获得空指针。

    // dynamic_cast
    #include 
    #include 
    using namespace std;
    
    class Base { virtual void dummy() {} };
    class Derived: public Base { int a; };
    
    int main () {
      try {
        Base * pba = new Derived;
        Base * pbb = new Base;
        Derived * pd;
    
        pd = dynamic_cast<Derived*>(pba);
        if (pd==0) cout << "Null pointer on first type-cast.\n";
    
        pd = dynamic_cast<Derived*>(pbb);
        if (pd==0) cout << "Null pointer on second type-cast.\n";
    
      } catch (exception& e) {cout << "Exception: " << e.what();}
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    reinterpret_cast

    二进制内存形式的强制转换

    class A { /* ... */ };
    class B { /* ... */ };
    A * a = new A;
    B * b = reinterpret_cast(a);
    
    • 1
    • 2
    • 3
    • 4

    static_cast

    显示类型转换

    class Base {};
    class Derived: public Base {};
    Base * a = new Base;
    Derived * b = static_cast<Derived*>(a)
    
    • 1
    • 2
    • 3
    • 4

    const_cast

    const_cast可以用于const关键字的去除

    // const_cast
    #include 
    using namespace std;
    
    void print (char * str)
    {
      cout << str << '\n';
    }
    
    int main () {
      const char * c = "sample text";
      print ( const_cast<char *> (c) );
      return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    柚子树环割机设计
    自动化测试基础——Pytest框架之YAML详解以及Parametrize数据驱动
    智慧驿站:为城市带来全新智慧公厕未来形态
    AcWing:第56场周赛
    安卓多渠道打包(五)360加固walle多渠道打包
    JUC并发编程系列详解篇六(死锁的基本概念)
    海上船舶交通事故VR模拟体验低成本高效率-深圳华锐视点
    XCTF1-web unseping
    kafka—offset偏移量
    IDA Pro正版多少钱?本文告诉你!
  • 原文地址:https://blog.csdn.net/yjkhtddx/article/details/126073518
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号