码农知识堂 - 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
  • 相关阅读:
    C语言水平测试题 过关斩将(3)辗转相除法,前n项求和,整数的正序分解,求最大公约数
    Web自动化测试进阶:网页中难点之等待机制 —— 强制等待,隐式等待
    AOF是什么?
    幻兽帕鲁服务器如何安装模组安装
    数组去重方法总结(记录)
    JavaScript 28 JavaScript 数组 Const
    【python】 16进制字符串转list
    「React | 网站部署」如何在云服务器上部署React并通过Nginx开放外网访问
    h264编码流程分析
    合成复用原则
  • 原文地址:https://blog.csdn.net/yjkhtddx/article/details/126073518
  • 最新文章
  • OpenClaw.NET 外部 CLI 预设系统:从零编写第三方 CLI 集成指南
    二、OpenCloudOS Server 9 系统 安装 Nginx
    AI 开发狂飙!.NET 11 Preview 4 原生集成向量搜索 + MCP 模板,EF Core 直接对标 RAG 应用
    Vibe Coding有多强?我只花了一天,就搓出了这个银行开户行查询网站!
    C# ESP32/STM32 轻量 Web 能力库:PicoServer.Nano
    告别手动计算,SymPy 初识与 Manim 联动
    Git实战覆盖98%日常开发场景
    CC5 反序列化链分析
    踩坑实录:读写分离导致批量删除逻辑问题
    C#如何优雅处理引用类型的深拷贝
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号