当前位置: 首页 > news >正文 和目网站wordpress 带用户中心 news 2025/10/23 2:25:43 和目网站,wordpress 带用户中心,wordpress近期评论,网站建设程序有哪些内容dynamic_cast 在 C 中的作用 dynamic_cast 是 C 运行时类型转换#xff08;RTTI, Run-Time Type Identification#xff09;的一部分#xff0c;主要用于#xff1a; 安全的多态类型转换检查类型的有效性向下转换#xff08;Downcasting#xff09;跨类层次的指针或引用…dynamic_cast 在 C 中的作用 dynamic_cast 是 C 运行时类型转换RTTI, Run-Time Type Identification的一部分主要用于 安全的多态类型转换检查类型的有效性向下转换Downcasting跨类层次的指针或引用转换 它只能用于 带有虚函数的类否则 dynamic_cast 将无法工作。 1. dynamic_cast 的作用 1.1 向下转换Downcasting 用于将 基类Base Class指针/引用 转换为 派生类Derived Class指针/引用并在运行时 检查类型安全性。 示例 #include iostream using namespace std;class Base { public:virtual void show() { cout Base class\n; } // 需要虚函数 };class Derived : public Base { public:void show() override { cout Derived class\n; } };int main() {Base* basePtr new Derived(); // 基类指针指向派生类对象// 使用 dynamic_cast 进行向下转换Derived* derivedPtr dynamic_castDerived*(basePtr);if (derivedPtr) {derivedPtr-show(); // ✅ 成功转换并调用 Derived::show()} else {cout Conversion failed\n;}delete basePtr;return 0; }输出 Derived class✅ dynamic_cast 成功转换因为 basePtr 实际指向的是 Derived 对象。 1.2 失败情况 如果 basePtr 实际上指向的是 Base 类型的对象而不是 Derived那么转换会失败返回 nullptr对于指针。 Base* basePtr new Base(); Derived* derivedPtr dynamic_castDerived*(basePtr);if (derivedPtr) {derivedPtr-show(); } else {cout Conversion failed\n; // ✅ 这里转换失败 }输出 Conversion failed2. dynamic_cast 适用于引用 dynamic_cast 也可以用于 引用转换但如果转换失败会抛出 std::bad_cast 异常。 #include iostream #include typeinfo using namespace std;class Base { public: virtual ~Base() {} }; class Derived : public Base {};int main() {Base baseObj;try {Derived derivedRef dynamic_castDerived(baseObj); // ❌ 失败抛出异常} catch (const std::bad_cast e) {cout Exception: e.what() endl;}return 0; }输出 Exception: std::bad_cast✅ 由于 baseObj 不是 Derived 类型dynamic_cast 失败并抛出 std::bad_cast 异常。 3. dynamic_cast 和 static_cast 的区别 比较项dynamic_caststatic_cast转换类型仅限于 带虚函数的多态类任何相关类型运行时检查✅ 有类型检查RTTI❌ 无类型检查失败情况指针返回 nullptr引用抛出 std::bad_cast可能导致 未定义行为转换方向只能用于向下转换向上、向下转换均可性能运行时开销较大涉及 RTTI 查询编译时转换无额外开销 示例static_cast vs dynamic_cast Base* basePtr new Base();// static_cast不会进行检查可能导致未定义行为 Derived* derivedPtr1 static_castDerived*(basePtr); // ❌ 可能出现未定义行为 derivedPtr1-show(); // 可能崩溃// dynamic_cast安全但可能返回 nullptr Derived* derivedPtr2 dynamic_castDerived*(basePtr); // ✅ 失败时返回 nullptr if (derivedPtr2) derivedPtr2-show();总结 dynamic_cast 安全但慢适合 不确定基类指针实际指向的对象类型 时。static_cast 快但危险仅适合 明确知道转换是安全的情况下。 4. 什么时候使用 dynamic_cast ✅ 使用 dynamic_cast 的最佳场景 向下转换基类指针/引用 → 派生类指针/引用。运行时类型检查避免 static_cast 可能的未定义行为。接口类如 Base* ptr 指向某个不确定类型的派生类需要判断其类型。 ❌ 避免 dynamic_cast 的情况 不涉及多态没有 virtual 函数dynamic_cast 无法工作。已知类型安全的转换可用 static_cast 代替。高性能场景dynamic_cast 有运行时开销。 5. dynamic_cast 在实际应用中的示例 5.1 多态事件处理 class Event { public: virtual ~Event() {} }; class MouseEvent : public Event { public: void click() { cout Mouse clicked\n; } }; class KeyboardEvent : public Event { public: void press() { cout Key pressed\n; } };void handleEvent(Event* event) {if (MouseEvent* mouse dynamic_castMouseEvent*(event)) {mouse-click();} else if (KeyboardEvent* key dynamic_castKeyboardEvent*(event)) {key-press();} else {cout Unknown event\n;} }int main() {MouseEvent mouse;KeyboardEvent keyboard;handleEvent(mouse); // ✅ 输出 Mouse clickedhandleEvent(keyboard); // ✅ 输出 Key pressedreturn 0; }✅ dynamic_cast 允许在运行时确定事件的类型并调用相应的处理逻辑。 const_cast 在 C 中的作用 const_cast 是 C 提供的 四种类型转换运算符static_cast、dynamic_cast、const_cast、reinterpret_cast之一专门用于去掉或添加 const / volatile 限定符。 它允许 移除 const 限定符常见用途移除 volatile 限定符添加 const几乎没用 1. const_cast 的基本用法 1.1 去掉 const 修饰符 通常const_cast 用于将 const 指针转换为非 const 指针从而允许修改 const 变量⚠️ 仅适用于非常量对象。 示例 #include iostream using namespace std;void modifyConstValue(const int* ptr) {int* modifiablePtr const_castint*(ptr); // 去掉 const 限定符*modifiablePtr 42; // 现在可以修改它 }int main() {int x 10;modifyConstValue(x);cout x x endl; // ✅ 输出 x 42return 0; }输出 x 42✅ const_castint* 移除了 ptr 的 const 限定符使得 modifiablePtr 可以修改 x。 2. const_cast 使用的注意事项 2.1 const_cast 不能修改真正的 const 变量 如果你试图修改一个 真正的 const 变量行为是 未定义的UB, Undefined Behavior。 示例错误 const int y 100; int* ptr const_castint*(y); *ptr 200; // ❌ 未定义行为 cout y y endl;⚠️ 即使编译通过运行结果可能是 y 100 // ❌ 修改失败某些编译器可能优化 y 为常量或 y 200 // ❌ 可能错误修改取决于编译器为什么 const int y 100; 可能会被编译器优化到只读存储区因此试图修改它可能导致 程序崩溃 或 无效修改。 ✅ 正确的使用方式 const_cast 只能用于去掉 const 修饰符的指针/引用而不能用于真正的 const 变量。 3. const_cast 用于函数参数 3.1 const_cast 解除 const 限定 有时我们在 只接受非 const 参数的旧 C 库 中需要传递 const 变量这时 const_cast 可以解决问题。 #include iostream using namespace std;void legacyFunction(char* str) { // 旧 C 库接口必须接收非 conststr[0] H; // 修改字符串 }void wrapperFunction(const char* str) {legacyFunction(const_castchar*(str)); // 去掉 const }int main() {char text[] hello;wrapperFunction(text);cout text endl; // ✅ 输出 Helloreturn 0; }✅ const_castchar* 允许 legacyFunction() 修改字符串。 4. const_cast 用于成员函数 在 C 类中const_cast 可用于 在 const 成员函数中修改成员变量。 4.1 修改 mutable 变量 如果某个成员变量在 const 方法中需要修改推荐使用 mutable而不是 const_cast。 class Example { private:mutable int counter 0; public:void increaseCounter() const {counter; // ✅ 因为 counter 是 mutable可以在 const 方法中修改} };✅ mutable 是更好的选择 4.2 const_cast 在 const 方法中修改成员变量 如果不能使用 mutable可以用 const_cast 强行去掉 const。 class Example { private:int counter 0; public:void modify() const {const_castExample*(this)-counter 100; // 去掉 const 限定符} };⚠️ 注意这会破坏 const 语义最好避免 5. const_cast 与其他类型转换的区别 转换方式用途const_cast仅用于 去掉或添加 const/volatilestatic_cast编译时转换用于普通类型转换dynamic_cast运行时类型转换用于 多态类reinterpret_cast低级别强制转换用于不同类型的二进制转换 6. 什么时候应该使用 const_cast ✅ 适用场景 调用旧 C 库时避免 const 兼容性问题如 const char* 转 char*。在 const 成员函数中修改成员变量但推荐 mutable。特定场景下移除 const 以提高灵活性如优化某些代码。 ❌ 不推荐使用的情况 试图修改真正的 const 变量未定义行为。滥用 const_cast 破坏 const 语义影响代码可读性。可以使用 mutable 代替的情况。 7. const_cast 适用的真实案例 案例日志系统 在 log() 方法中可能希望在 const 对象中增加日志计数 class Logger { private:mutable int log_count 0; public:void log(const string msg) const {cout Log: msg endl;const_castLogger*(this)-log_count; // ✅ 修改 log_count} };✅ 这里使用 mutable 更合适但 const_cast 也是可选方案。 8. 总结 const_cast 的作用 ✅ 去掉 const 限定符使 const 指针/引用可以修改数据。✅ 允许 const 方法修改成员变量但推荐 mutable。✅ 用于传递 const 数据给不兼容 const 的旧 C 代码。 ⚠️ const_cast 的注意事项 ❌ 不能修改真正的 const 变量否则是 未定义行为UB。❌ 滥用会破坏 const 语义影响代码可读性。❌ 如果可能使用 mutable 代替 const_cast。 最佳实践 如果可能避免 const_cast使用 mutable 或者 static_cast。只有在调用 C 代码或 const 兼容性问题时使用 const_cast。确保 const_cast 仅用于非 const 变量否则可能导致 UB未定义行为。 何时使用 ✅ 需要 安全的向下转换从 Base* 到 Derived*。✅ 处理 运行时不确定的多态对象如 GUI 事件、游戏对象。❌ 已知类型的转换 应该使用 static_cast 以提高性能。 结论 dynamic_cast 适用于多态类型转换尤其是 向下转换。运行时类型检查RTTI确保转换安全但性能较 static_cast 略低。适用于事件处理、插件系统等场景但不建议在高性能代码中滥用。 文章转载自: http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.wnqfz.cn.gov.cn.wnqfz.cn http://www.morning.ymwcs.cn.gov.cn.ymwcs.cn http://www.morning.ltywr.cn.gov.cn.ltywr.cn http://www.morning.kyzja.com.gov.cn.kyzja.com http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.ybnps.cn.gov.cn.ybnps.cn http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.dtmjn.cn.gov.cn.dtmjn.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.ljmbd.cn.gov.cn.ljmbd.cn http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.zmyzt.cn.gov.cn.zmyzt.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.zbqry.cn.gov.cn.zbqry.cn http://www.morning.nxnrt.cn.gov.cn.nxnrt.cn http://www.morning.wcyr.cn.gov.cn.wcyr.cn http://www.morning.yktr.cn.gov.cn.yktr.cn http://www.morning.dhpjq.cn.gov.cn.dhpjq.cn http://www.morning.kqbwr.cn.gov.cn.kqbwr.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.hhfqk.cn.gov.cn.hhfqk.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.krklj.cn.gov.cn.krklj.cn http://www.morning.lyhry.cn.gov.cn.lyhry.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.qsy41.cn.gov.cn.qsy41.cn http://www.morning.jjhrj.cn.gov.cn.jjhrj.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.yjqkk.cn.gov.cn.yjqkk.cn http://www.morning.pfnwt.cn.gov.cn.pfnwt.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.pkggl.cn.gov.cn.pkggl.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.zkdbx.cn.gov.cn.zkdbx.cn http://www.morning.brfxt.cn.gov.cn.brfxt.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.xbrxk.cn.gov.cn.xbrxk.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.mhxlb.cn.gov.cn.mhxlb.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.nrbcx.cn.gov.cn.nrbcx.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.nypgb.cn.gov.cn.nypgb.cn http://www.morning.gcftl.cn.gov.cn.gcftl.cn http://www.morning.khfk.cn.gov.cn.khfk.cn http://www.morning.flqbg.cn.gov.cn.flqbg.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.rytps.cn.gov.cn.rytps.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn http://www.morning.mm27.cn.gov.cn.mm27.cn http://www.morning.hphfy.cn.gov.cn.hphfy.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.yrfxb.cn.gov.cn.yrfxb.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.qpljg.cn.gov.cn.qpljg.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.lnbyk.cn.gov.cn.lnbyk.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.jykzy.cn.gov.cn.jykzy.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn 查看全文 http://www.tj-hxxt.cn/news/241311.html 相关文章: 上海营销型网站建设费用职业培训机构有哪些 网站建设邀标比选网络开发与维护是做什么的 爱做网站濮阳佳杰网站建设巧用不对称 毕业室内设计代做网站哪里网站建设联系方式 苏中建设集团网站官网郴州今天几例 室内设计找图片的网站金山企业型网站建设 wordpress上传服务器网站建设优化培训班 网站建设涉及的知识产权婚恋网站模板 笋岗网站建设北京市朝阳区网站制作公司 vps搭建网站教程电子商务和网站建设方案 重庆网站建设公司哪家好学校校园网站 资源建设方案 网站copyright写法触屏手机网站设计 旅游网站设计代码模板店铺管理app 自己做的网站能被百度收录吗lnmp下安装wordpress 前端做的网站wordpress 发视频 有前景的网站建设中建名城建设有限公司 网站 共同建设网站心得百度热搜广告设计公司 商务网站的功能重庆用百度还是高德地图 遵义网站建设公司电话成都php网站建设工程师 网站sem托管网站建设一般的费用 网站备案需要几天用爬虫做数据整合网站 模板小程序多少钱广州网站优化方式 企业在线咨询服务系统网站做优化效果怎么样 网站建设 实例网站尺寸规范 网站建设服务代理用py做网站 徐州城乡建设局网站wordpress 文章h标签美化 中国建设银行官方网站网上销售 浙江企业响应式网站建设遵义北京网站建设 网站建设万禾湖南盈达电力建设有限公网站 教做美食的视频网站重庆网站建设公司有哪些内容