码农知识堂 - 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
  • 相关阅读:
    Unity笔记(15):OnTriggerEnter2D [2D]
    基于FPGA的LCD1602驱动(含代码)
    漫谈计算机网络:网络层 ------ 重点:IP协议与互联网路由选择协议
    <模板进阶>——《C++高阶》
    Spring Cloud Alibaba微服务从入门到进阶(四)(服务发现-Nacos )
    MyBatis、MyBatisPlus转义数据库关键字
    常见html+css面试题
    因果图测试用例设计方法介绍(超全的总结笔记错过就没有了)
    计算机的六种连接方式
    vue3.0 axios封装
  • 原文地址:https://blog.csdn.net/yjkhtddx/article/details/126073518
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号