当前位置: 首页 > news >正文

C++ 快速回顾(六)

C++ 快速回顾(六)

  • 前言
  • 一、const_cast
  • 二、reinterpret_cast
  • 三、dynamic_cast
  • 四、static_cast


前言

用于快速回顾之前遗漏或者补充C++知识


在这里插入图片描述

一、const_cast

const_cast主要用于添加或移除const(或volatile)属性。它不改变类型本身,只改变类型的常量性。注意:移除常量性的操作必须谨慎,因为修改一个原本定义为常量的对象会导致未定义行为。

如下是正常的转换:

class FTestClass
{
public:void Init() {}
};int main()
{const FTestClass* Ptr = new FTestClass();//Ptr->Init();FTestClass* NotConstPtr1 = const_cast<FTestClass*>(Ptr);NotConstPtr1->Init();FTestClass* NotConstPtr2 = (FTestClass*)Ptr;NotConstPtr2->Init();system("pause");return 0;
}

如下是错误的转换

	const int a = 10;int* aa = const_cast<int*>(&a);*aa = 20; // 错误!!! 无法修改std::cout << a; // 这里还是10

二、reinterpret_cast

reinterpret_cast提供低层次的重新解释位模式的转换。它可以将任意指针类型转换为另一个指针类型,或者将指针和整数之间转换。这种转换非常不安全,因为它不进行任何类型检查。通常用于底层编程,如驱动程序和需要直接操作内存的场合。

int main()
{FTestClass* Ptr = new FTestClass();// 转换为int值 再 转换回去int value = reinterpret_cast<int>(Ptr);FTestClass* NewValue1 = reinterpret_cast<FTestClass*>(value);NewValue1->Init();// 转换为int指针 再 转换回去int* IntPtr = reinterpret_cast<int*>(Ptr);FTestClass* NewValue2 = reinterpret_cast<FTestClass*>(IntPtr);NewValue2->Init();system("pause");return 0;
}

三、dynamic_cast

dynamic_cast主要用于在类层次结构中进行安全的向下转换(即基类指针或引用转换为派生类)以及跨继承转换(如多继承中的兄弟类之间的转换)。它需要运行时类型信息(RTTI)支持,因此只能用于多态类型(即类至少有一个虚函数)。如果转换失败,对于指针转换返回nullptr,对于引用转换则抛出std::bad_cast异常。

注意:dynamic_cast 是安全的转换,当转换失败时会返回空指针,并不会变成野指针

class FTestClass
{
public:void BaseFunc(){}//dynamic_cast 必须有虚函数才能转换virtual void Init(){}
};class FTestChildClass : public FTestClass
{
public:void ChildFunc(){}
};int main()
{FTestClass* Ptr = new FTestChildClass();FTestChildClass* ChildPtr = dynamic_cast<FTestChildClass*>(Ptr);ChildPtr->BaseFunc();ChildPtr->ChildFunc();FTestClass* Ptr1 = new FTestClass();FTestChildClass* ChildPtr1 = dynamic_cast<FTestChildClass*>(Ptr1);if (ChildPtr1 == nullptr){printf("is null.\r\n");}system("pause");return 0;
}

四、static_cast

static_cast是最常用的转换,用于非多态类型的转换。它可以在相关类型之间进行转换,比如整数到浮点数,或者指针在类层次结构中的向上转换(即基类指针指向派生类对象,这是安全的)。它也可以用于任何隐式转换的逆转换(例如,将void*转换为其他指针类型,或者将基类指针转换为派生类指针,但后者是不安全的,需要程序员确保安全)

class FTestClass
{
public:void BaseFunc(){printf("Base\r\n");}};class FTestChildClass : public FTestClass
{
public:void ChildFunc(){printf("Child\r\n");printf("%d\r\n", a);}int a = 10;
};int main()
{FTestClass* Ptr = new FTestChildClass();FTestChildClass* ChildPtr = static_cast<FTestChildClass*>(Ptr);ChildPtr->BaseFunc();ChildPtr->ChildFunc();FTestClass* Ptr1 = new FTestClass();FTestChildClass* ChildPtr1 = static_cast<FTestChildClass*>(Ptr1); // 转换失败不会为空,会变成野指针ChildPtr1->BaseFunc();//这里的函数理论上转换失败为什么还能调用?// 是因为函数是在编译时就确定好了,但是如果访问其中的变量值就会出现问题ChildPtr1->ChildFunc();system("pause");return 0;
}
http://www.dtcms.com/a/264404.html

相关文章:

  • 设计模式精讲 Day 22:模板方法模式(Template Method Pattern)
  • Coze(扣子):基础学习
  • Python应用指南:利用高德地图API获取公交+地铁可达圈(二)
  • OpenCV图像梯度处理详解:原理、API与实战代码解析
  • 【Cyberstrikelab】lab3
  • AngularJS 安装使用教程
  • 转矩常数KT
  • 什么是数据孤岛?如何解决数据孤岛问题?
  • Wisdom SSH 与宝塔面板:深度对比剖析
  • 机器学习在智能教育中的应用:个性化学习路径与学习效果评估
  • socket编程
  • JavaEE线程概念
  • Git 运行.sh文件
  • js filter()
  • Linux 终止进程
  • 【ArcGIS】矢量数据的叠加分析
  • 面试拷打-20250701
  • LLM面试12
  • vite项目中引入tailwindcss,难倒AI的操作
  • day48
  • 目前最火的agent方向-A2A快速实战构建(二): AutoGen模型集成指南:从OpenAI到本地部署的全场景LLM解决方案
  • C语言实战:2048数字合并游戏
  • 【C++】头文件的能力与禁忌
  • [Python 基础课程]数字
  • wrap+aria2c提高下载速度
  • 创宇智脑 MCP 赋能 AiPy,IP 风险调查效率实现 10 倍飞跃,威胁分析一键生成
  • c语言中的函数I
  • NV103NV105美光固态闪存NV107NV108
  • Python OrderedDict 用法详解
  • 【1.7 漫画Java核心并发编程】