• 【C++类型转换】4种类型转换:static_cast、reinterpret_cast、const_cast、dynamic_cast


    目录

    1. C语言中的类型转换

    2.1.类型转换:static_cast

    2.2.类型转换:reinterpret_cast

    2.3.类型转换:const_cast

    2.4.类型转换:dynamic_cast


    1. C语言中的类型转换

    1. 隐式类型转换:编译器在编译阶段自动进行,能转就转,不能转就编译失败(相近类型:(整形和浮点)、(指针和指针))
    2. 显式类型转化:非相近类型强制类型转换
    1. void Test ()
    2. {
    3. int i = 1;
    4. // 隐式类型转换
    5. double d = i;
    6. printf("%d, %.2f\n" , i, d);
    7. int* p = &i;
    8. // 显示的强制类型转换
    9. int address = (int) p;
    10. printf("%x, %d\n" , p, address);
    11. }

     c语言隐式类型转换会发生的一些问题

    • 下面代码i和j比较会把i隐式类型转换为unsigned int,和j比较不会小于0死循环
    1. int main()
    2. {
    3. int i = 5; size_t j = 0;
    4. while (i >= j)
    5. {
    6. i--;
    7. if (i == -10)
    8. break;
    9. cout << i << " ";
    10. }
    11. return 0;
    12. }

     所以C++提出了中类型转换来规范

    2.1.类型转换:static_cast

    static_cast对应的是C语言中的隐式类型转换,支持相近类型的转换,非相近类型编译不通过;

    1. void Test1()
    2. {
    3. int i = 1;
    4. // sattic_cast
    5. double d = static_cast<double>(i);
    6. printf("%d, %.2f\n", i, d);
    7. }
    8. int main()
    9. {
    10. Test1();
    11. return 0;
    12. }

     

     2.2.类型转换:reinterpret_cast

     reinterpret_cast对应的是C语言中的显示类型转换,支持非相近类型的转换

    1. void Test1()
    2. {
    3. int i = 1;
    4. // reinterpret_cast
    5. int* p = &i;
    6. int address = reinterpret_cast<int>(p);
    7. printf("%x, %d\n", p, address);
    8. }
    9. int main()
    10. {
    11. Test1();
    12. return 0;
    13. }

     2.3.类型转换:const_cast

    const_cast最常用的用途就是删除变量的const属性,方便赋值 

    1. int main()
    2. {
    3. const int a = 5;
    4. int* p = const_cast<int*>(&a);
    5. *p = 10;
    6. cout << a << endl;
    7. cout << *p << endl;
    8. return 0;
    9. }

    上面代码执行结果

    原因:编译器都被const修饰的变量有优化;不会取内存中取,会直接使用常量5; 

     关键字volatile:不让编译器优化,去内存中取值

     常量区禁止写入,编译好的指令在常量区

    1. volatile const int a = 0;//常量区不能写入,编译好的指令在常量区
    2. int main()
    3. {
    4. int* p = const_cast<int*>(&a);
    5. *p = 10;
    6. cout << a << endl;
    7. cout << *p << endl;
    8. return 0;
    9. }

     2.4.类型转换:dynamic_cast

    1. 向上转型:子类对象指针/引用->父类指针/引用(不需要转换,赋值兼容规则)(切片行为)
    2. 向下转型:父类对象指针/引用->子类指针/引用(用dynamic_cast转型是安全的)

    要求:

    1.  dynamic_cast只能用于父类含有虚函数的类(必须是多态,因为虚函数表内存了一个判断它是子类还是父类)
    2. dynamic_cast会先检查是否能转换成功,能成功则转换,不能则返回nullptr

     需求:如果pa是指向父类对象,那么不做任何处理,如果pa是指向子类对象,那么请转回子类,并打印子类对象地址

    1. class A
    2. {
    3. virtual void f() {}
    4. public:
    5. };
    6. class B : public A
    7. {
    8. public:
    9. };
    10. void func(A* pa)//切片
    11. {
    12. // dynamic_cast--如果pa指向的父类对象,那么则转换不成功,返回nullptr
    13. // dynamic_cast--如果pa指向的子类对象,那么则转换成功,返回对象指针
    14. B* pb1 = dynamic_cast(pa);
    15. if (pb1 == nullptr)
    16. {
    17. cout << "转换失败" << endl;
    18. }
    19. else
    20. {
    21. cout << "pb1:" << pb1 << endl;
    22. }
    23. }
    24. int main()
    25. {
    26. A aa;
    27. B bb;
    28. func(&aa);
    29. func(&bb);
    30. }

     

  • 相关阅读:
    js为什么是单线程?
    paddledetection在window使用cpu快速上手 & 在cpu端训练自己的VOC类型数据集
    Verilog中函数function的使用
    Transformer学习
    Charles-ios无法抓包原因之一证书
    微信公众号后台管理
    【Redis】数据结构---String
    量子计算:未来技术的变革与应用
    Gitlab 降级(reconfigure 失败)
    Vue实现分页功能
  • 原文地址:https://blog.csdn.net/m0_72964546/article/details/128204747