{"id":389,"date":"2024-06-14T19:46:38","date_gmt":"2024-06-14T11:46:38","guid":{"rendered":"https:\/\/www.vanforever.com.cn\/?p=389"},"modified":"2024-06-14T19:46:38","modified_gmt":"2024-06-14T11:46:38","slug":"%e4%ba%86%e8%a7%a3c%e4%b8%ad%e7%9a%84%e5%9b%9b%e7%a7%8d-cast","status":"publish","type":"post","link":"https:\/\/www.vanforever.com.cn\/?p=389","title":{"rendered":"\u4e86\u89e3C++\u4e2d\u7684\u56db\u79cd Cast"},"content":{"rendered":"<div  class='collapse-block shadow-sm collapse-block-transparent collapsed hide-border-left'><div class='collapse-block-title'><span class='collapse-block-title-inner'>\u67e5\u770b\u7279\u8272\u56fe\u7247<\/span><i class='collapse-icon fa fa-angle-down'><\/i><\/div><div class='collapse-block-body' style='display:none;'><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic.vanforever.com.cn\/Vanforever\/20240610\/wallhaven-2yxp16_3840x2400.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large\" data-original=\"https:\/\/pic.vanforever.com.cn\/Vanforever\/20240610\/wallhaven-2yxp16_3840x2400.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" width=\"3840\" height=\"2400\" \/><\/div><\/div><\/div>\n<h1 id=\"\u4e86\u89e3c\u4e2d\u7684\u56db\u79cd-cast\">\u4e86\u89e3C++\u4e2d\u7684\u56db\u79cd Cast<\/h1>\n<h2 id=\"staticcast\">static_cast<\/h2>\n<p><code>static_cast<\/code> \u662f C++ \u4e2d\u7528\u4e8e\u6267\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u7684\u64cd\u4f5c\u7b26\uff0c\u5b83\u5141\u8bb8\u4f60\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u800c\u4e0d\u662f\u5728\u8fd0\u884c\u65f6\u3002<code>static_cast<\/code> \u901a\u5e38\u7528\u4e8e\u6267\u884c\u5b89\u5168\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u6bd4\u5982\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u3001\u7c7b\u5c42\u6b21\u7ed3\u6784\u4e2d\u7684\u5411\u4e0a\u8f6c\u6362\uff08\u5c06\u6d3e\u751f\u7c7b\u6307\u9488\u6216\u5f15\u7528\u8f6c\u6362\u4e3a\u57fa\u7c7b\u6307\u9488\u6216\u5f15\u7528\uff09\uff0c\u4ee5\u53ca\u5411\u4e0b\u8f6c\u6362\uff08\u5728\u4f60\u77e5\u9053\u7c7b\u578b\u786e\u5b9e\u5339\u914d\u7684\u60c5\u51b5\u4e0b\uff0c\u5c06\u57fa\u7c7b\u6307\u9488\u6216\u5f15\u7528\u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u6307\u9488\u6216\u5f15\u7528\uff09\u3002<\/p>\n<ol start=\"\">\n<li><strong>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-cpp\" lang=\"cpp\">int a = 10;\nchar c = static_cast&lt;char&gt;(a); \/\/ int \u5230 char \u7684\u8f6c\u6362\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u7c7b\u5c42\u6b21\u7ed3\u6784\u4e2d\u7684\u5411\u4e0a\u8f6c\u6362\uff08\u5b89\u5168\uff09\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-cpp\" lang=\"cpp\">class Base {};\nclass Derived : public Base {};\n\nDerived* d = new Derived();\nBase* b = static_cast&lt;Base*&gt;(d); \/\/ Derived* \u5230 Base* \u7684\u8f6c\u6362\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>\u7c7b\u5c42\u6b21\u7ed3\u6784\u4e2d\u7684\u5411\u4e0b\u8f6c\u6362\uff08\u4e0d\u5b89\u5168\uff0c\u9664\u975e\u4f60\u77e5\u9053\u786e\u5b9e\u7c7b\u578b\u5339\u914d\uff09\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-cpp\" lang=\"cpp\">Base* b = new Derived();\nDerived* d = static_cast&lt;Derived*&gt;(b); \/\/ Base* \u5230 Derived* \u7684\u8f6c\u6362\n\/\/ \u53ea\u6709\u5f53\u4f60\u786e\u4fe1 b \u6307\u5411\u7684\u662f Derived \u5bf9\u8c61\u65f6\uff0c\u8fd9\u79cd\u5411\u4e0b\u8f6c\u6362\u624d\u662f\u5b89\u5168\u7684\n\/\/\u5982\u679c\u4f60\u9700\u8981\u786e\u4fdd\u5b89\u5168\u6027\uff0c\u5e94\u8be5\u4f7f\u7528 dynamic_cast\uff0c\u5b83\u4f1a\u68c0\u67e5\u8f6c\u6362\u662f\u5426\u5408\u6cd5\uff0c\u5e76\u5728\u4e0d\u5408\u6cd5\u65f6\u8fd4\u56de nullptr\uff08\u5bf9\u4e8e\u6307\u9488\uff09\u6216\u629b\u51fa std::bad_cast \u5f02\u5e38\uff08\u5bf9\u4e8e\u5f15\u7528\uff09\u3002\n<\/code><\/pre>\n<ol start=\"4\">\n<li><strong>\u5c06 <code>void*<\/code> \u8f6c\u6362\u4e3a\u5177\u4f53\u7c7b\u578b\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-cpp\" lang=\"cpp\">void* ptr = ...;\nint* intPtr = static_cast&lt;int*&gt;(ptr); \/\/ void* \u5230 int* \u7684\u8f6c\u6362\n<\/code><\/pre>\n<p>\u4f7f\u7528 <code>static_cast<\/code> \u65f6\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li><code>static_cast<\/code> \u4e0d\u4f1a\u8fdb\u884c\u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5\uff0c\u56e0\u6b64\u5982\u679c\u8f6c\u6362\u4e0d\u6b63\u786e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002<\/li>\n<li><code>static_cast<\/code> \u4e0d\u80fd\u7528\u4e8e\u5220\u9664 <code>const<\/code>\u3001<code>volatile<\/code> \u6216 <code>__unaligned<\/code> \u4fee\u9970\u7b26\uff0c\u5982\u679c\u8981\u53bb\u9664\u8fd9\u4e9b\u4fee\u9970\u7b26\uff0c\u5e94\u8be5\u4f7f\u7528 <code>const_cast<\/code>\u3002<\/li>\n<li><code>static_cast<\/code> \u4e0d\u80fd\u7528\u4e8e\u5c06\u975e\u7c7b\u7c7b\u578b\u8f6c\u6362\u4e3a\u7c7b\u7c7b\u578b\uff0c\u8fd9\u79cd\u8f6c\u6362\u901a\u5e38\u662f\u4e0d\u5b89\u5168\u7684\u3002<\/li>\n<\/ul>\n<h2 id=\"dynamiccast\">dynamic_cast<\/h2>\n<p><code>dynamic_cast<\/code> \u662f C++ \u4e2d\u7528\u4e8e\u5728\u7c7b\u5c42\u6b21\u7ed3\u6784\u4e2d\u8fdb\u884c\u5b89\u5168\u5411\u4e0b\u8f6c\u6362\uff08downcasting\uff09\u7684\u64cd\u4f5c\u7b26\u3002\u5b83\u5141\u8bb8\u4f60\u5c06\u57fa\u7c7b\u6307\u9488\u6216\u5f15\u7528\u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u6307\u9488\u6216\u5f15\u7528\uff0c\u5e76\u4e14\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5\uff08RTTI\uff09\u3002\u5982\u679c\u8f6c\u6362\u662f\u4e0d\u5408\u6cd5\u7684\uff0c\u5373\u57fa\u7c7b\u6307\u9488\u5b9e\u9645\u4e0a\u5e76\u4e0d\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\uff0c\u90a3\u4e48 <code>dynamic_cast<\/code> \u4f1a\u8fd4\u56de <code>nullptr<\/code>\uff08\u5bf9\u4e8e\u6307\u9488\uff09\u6216\u629b\u51fa <code>std::bad_cast<\/code> \u5f02\u5e38\uff08\u5bf9\u4e8e\u5f15\u7528\uff09\u3002<\/p>\n<h3 id=\"rtti\">RTTI<\/h3>\n<p>RTTI\uff08Run-Time Type Information\uff0c\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f\uff09\u662f C++ \u4e2d\u7684\u4e00\u79cd\u673a\u5236\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u83b7\u53d6\u5bf9\u8c61\u7684\u7c7b\u578b\u4fe1\u606f\u3002RTTI \u4e3b\u8981\u7528\u4e8e\u4e24\u4e2a\u65b9\u9762\uff1a\u7c7b\u578b\u8bc6\u522b\u548c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<ol start=\"\">\n<li>\u7c7b\u578b\u8bc6\u522b\uff1aRTTI \u5141\u8bb8\u7a0b\u5e8f\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u7684\u5177\u4f53\u7c7b\u578b\uff0c\u8fd9\u662f\u901a\u8fc7 <code>typeid<\/code> \u64cd\u4f5c\u7b26\u5b9e\u73b0\u7684\u3002<code>typeid<\/code> \u8fd4\u56de\u4e00\u4e2a <code>type_info<\/code> \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u6709\u5173\u7c7b\u578b\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5982\u7c7b\u578b\u540d\u79f0\u3002<\/li>\n<li>\u7c7b\u578b\u8f6c\u6362\uff1aRTTI \u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u5b89\u5168\u5730\u8f6c\u6362\u5bf9\u8c61\u7c7b\u578b\uff0c\u8fd9\u662f\u901a\u8fc7 <code>dynamic_cast<\/code> \u64cd\u4f5c\u7b26\u5b9e\u73b0\u7684\u3002<code>dynamic_cast<\/code> \u53ef\u4ee5\u5c06\u5bf9\u8c61\u6307\u9488\u6216\u5f15\u7528\u5b89\u5168\u5730\u8f6c\u6362\u4e3a\u76ee\u6807\u7c7b\u578b\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u5982\u679c\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u7c7b\u578b\u4e0d\u5339\u914d\uff0c\u5bf9\u4e8e\u6307\u9488\u8fd4\u56de <code>nullptr<\/code>\uff0c\u5bf9\u4e8e\u5f15\u7528\u629b\u51fa <code>std::bad_cast<\/code> \u5f02\u5e38\u3002<\/li>\n<\/ol>\n<p>\u5728RTTI\u7684\u5b9e\u73b0\u4e2d\uff0c<code>RTTICompleteObjectLocator<\/code>\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7ed3\u6784\u4f53\uff0c\u5b83\u5305\u542b\u4e86\u51e0\u4e2a\u5173\u952e\u6210\u5458\uff1a<\/p>\n<ol start=\"\">\n<li><code>typeinfo<\/code>\uff1a\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u7c7b\u578b\u540d\u548c\u7236\u7c7b\u7c7b\u578b\u540d\u7684\u7ed3\u6784\u4f53\uff0c\u901a\u8fc7<code>typeid<\/code>\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u5bf9\u8c61\u7684<code>typeinfo<\/code>\uff0c\u4ece\u800c\u5f97\u5230\u5bf9\u8c61\u7684\u7c7b\u578b\u4fe1\u606f\u3002<\/li>\n<li><code>class descriptor<\/code>\uff1a\u8fd9\u662f\u4e00\u4e2a\u63cf\u8ff0\u7ee7\u627f\u5173\u7cfb\u548c\u4fe1\u606f\u7684\u7ed3\u6784\u4f53\u3002\u5b83\u8bb0\u5f55\u4e86\u57fa\u7c7b\u7684\u4e2a\u6570\u3001\u57fa\u7c7b\u7684\u6570\u7ec4\u3001\u4ee5\u53ca\u8be5\u7c7b\u662f\u5426\u4f7f\u7528\u4e86\u591a\u7ee7\u627f\u6216\u865a\u7ee7\u627f\u3002<\/li>\n<li><code>offset<\/code>\uff1a\u8fd9\u662f\u865a\u51fd\u6570\u6307\u9488\u76f8\u5bf9\u4e8e\u7c7b\u5934\u90e8\u7684\u504f\u79fb\u91cf\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u4f18\u5316\u52a8\u6001\u7c7b\u578b\u8f6c\u6362\uff08\u5982<code>dynamic_cast<\/code>\uff09\u7684\u901f\u5ea6\uff0c\u56e0\u4e3a\u5f53\u5c06\u57fa\u7c7b\u7684\u6307\u9488\u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u7684\u6307\u9488\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7<code>this<\/code>\u6307\u9488\u52a0\u4e0a\u8fd9\u4e2a\u504f\u79fb\u91cf\u6765\u627e\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ol>\n<p>RTTI\u7684\u4f7f\u7528\u573a\u666f\u662f\uff0c\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u5e76\u4e0d\u77e5\u9053\u4e00\u4e2a\u6307\u9488\u6307\u5411\u7684\u662f\u5b50\u7c7b\u8fd8\u662f\u57fa\u7c7b\u5bf9\u8c61\uff0c\u5c31\u9700\u8981\u901a\u8fc7RTTI\u5f97\u5230\u5176\u4fe1\u606f\uff0c\u6bd4\u5982typeid\u548cdynamic_cast\u3002RTTI\u5b58\u5728\u4e8e\u6bcf\u4e2a\u865a\u51fd\u6570\u8868\u9996\u5730\u5740\u5f80\u4e0a\u7684\u56db\u4e2a\u5b57\u8282\uff0c\u6240\u4ee5\u7236\u7c7b\u81f3\u5c11\u8981\u6709\u4e00\u4e2a\u865a\u51fd\u6570\uff0c\u5426\u5219\u65e0\u6cd5\u8bc6\u522b\u6b63\u786e\u7c7b\u578b\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u70b9\uff1a<\/p>\n<ol start=\"\">\n<li><code>dynamic_cast<\/code> \u4ec5\u9002\u7528\u4e8e\u5305\u542b\u81f3\u5c11\u4e00\u4e2a\u865a\u51fd\u6570\u7684\u7c7b\u5c42\u6b21\u7ed3\u6784\uff0c\u56e0\u4e3a\u5b83\u662f\u901a\u8fc7\u865a\u51fd\u6570\u8868\u6765\u5b9e\u73b0\u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5\u7684\u3002<\/li>\n<li><code>dynamic_cast<\/code> \u7528\u4e8e\u5411\u4e0b\u8f6c\u6362\u65f6\u6bd4 <code>static_cast<\/code> \u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u4f1a\u68c0\u67e5\u8f6c\u6362\u7684\u5408\u6cd5\u6027\u3002<\/li>\n<li><code>dynamic_cast<\/code> \u7684\u6027\u80fd\u6210\u672c\u6bd4 <code>static_cast<\/code> \u9ad8\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u5728\u8fd0\u884c\u65f6\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u3002<\/li>\n<li><code>dynamic_cast<\/code> \u53ef\u4ee5\u7528\u4e8e\u4ea4\u53c9\u8f6c\u6362\uff0c\u5373\u5728\u4e0d\u540c\u7ee7\u627f\u5c42\u6b21\u7684\u6d3e\u751f\u7c7b\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/li>\n<li>\u6709\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u70b9\uff0c\u83f1\u5f62\u7ee7\u627f\u548c\u4fa7\u5411\u8f6c\u6362\u65f6\uff0c<code>dynamic_cast<\/code>\u53ef\u80fd\u4f1a\u56e0\u4e8c\u4e49\u6027\u9020\u6210\u8f6c\u6362\u5931\u8d25\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-cpp\" lang=\"cpp\">class Base {\npublic:\n    virtual void f() {}\n};\n\nclass Derived : public Base {\npublic:\n    void f() override {}\n};\n\nBase* b = new Derived();\nDerived* d = dynamic_cast&lt;Derived*&gt;(b); \/\/ \u5b89\u5168\u7684\u5411\u4e0b\u8f6c\u6362\nif (d) {\n    \/\/ \u8f6c\u6362\u6210\u529f\uff0cd \u6307\u5411 Derived \u5bf9\u8c61\n} else {\n    \/\/ \u8f6c\u6362\u5931\u8d25\uff0cb \u4e0d\u6307\u5411 Derived \u5bf9\u8c61\n}\n\n<\/code><\/pre>\n<h2 id=\"constcast\">const_cast<\/h2>\n<p><code>const_cast<\/code> \u662f C++ \u4e2d\u7528\u4e8e\u53bb\u9664 const \u7279\u6027\u7684\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26\u3002\u5b83\u5141\u8bb8\u4f60\u5c06 const \u5bf9\u8c61\u7684<strong>\u6307\u9488\u6216\u5f15\u7528<\/strong>\u8f6c\u6362\u4e3a\u975e const \u5bf9\u8c61\u7684\u6307\u9488\u6216\u5f15\u7528\uff0c\u4ece\u800c\u4f7f\u5f97\u539f\u672c\u4e0d\u53ef\u4fee\u6539\u7684\u5bf9\u8c61\u53d8\u5f97\u53ef\u4fee\u6539\u3002\u8fd9\u79cd\u8f6c\u6362\u662f\u5728\u7f16\u8bd1\u65f6\u8fdb\u884c\u7684\uff0c\u4f46\u5b83\u4e0d\u4f1a\u6539\u53d8\u5bf9\u8c61\u672c\u8eab\u7684 const \u6027\u8d28\uff0c\u53ea\u662f\u8ba9\u7f16\u8bd1\u5668\u5141\u8bb8\u4f60\u901a\u8fc7\u6307\u9488\u6216\u5f15\u7528\u4fee\u6539\u5bf9\u8c61\u3002<\/p>\n<p>\u53ea\u80fd\u4fee\u6539\u5e38\u91cf\u6307\u9488\uff0c\u4e0d\u80fd\u4fee\u6539\u6307\u9488\u5e38\u91cf\u3002<\/p>\n<ol start=\"\">\n<li><code>const_cast<\/code> \u4ec5\u7528\u4e8e\u53bb\u9664 const \u6216 volatile \u7279\u6027\uff0c\u4e0d\u80fd\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362\u3002<\/li>\n<li><code>const_cast<\/code> \u53ea\u9002\u7528\u4e8e\u6307\u9488\u6216\u5f15\u7528\uff0c\u4e0d\u80fd\u76f4\u63a5\u5e94\u7528\u4e8e\u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528 <code>const_cast<\/code> \u53bb\u9664 const \u7279\u6027\u662f\u5371\u9669\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bf9 const \u5bf9\u8c61\u7684\u610f\u5916\u4fee\u6539\uff0c\u4ece\u800c\u7834\u574f\u7a0b\u5e8f\u7684\u4e0d\u53d8\u6027\u548c\u5b89\u5168\u6027\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-cpp\" lang=\"cpp\">class MyClass {\npublic:\n    void modify() { \/* ... *\/ }\n};\n\n\nint main()\n{\n    const MyClass obj;\n    MyClass* nonConstPtr = const_cast&lt;MyClass*&gt;(&amp;obj);\n    nonConstPtr-&gt;modify(); \/\/ \u73b0\u5728 \u53ef\u4ee5\u8c03\u7528\u975e const \u6210\u5458\u51fd\u6570\u4e86\n\n    \/\/ \u4f7f\u7528 const_cast \u53bb\u9664 volatile \u7279\u6027\n    volatile int vi = 1;\n    int* nonVolatilePtr = const_cast&lt;int*&gt;(&amp;vi);\n    *nonVolatilePtr = 2; \/\/ \u73b0\u5728 \u53ef\u4ee5\u4fee\u6539 volatile \u53d8\u91cf\u4e86\n}\n<\/code><\/pre>\n<h2 id=\"reinterpretcast\">reinterpret_cast<\/h2>\n<p><code>reinterpret_cast<\/code> \u662f C++ \u4e2d\u7528\u4e8e\u6267\u884c\u975e\u7c7b\u578b\u5b89\u5168\u7684\u4f4e\u7ea7\u7c7b\u578b\u8f6c\u6362\u7684\u64cd\u4f5c\u7b26\u3002\u5b83\u5141\u8bb8\u4f60\u5c06\u4e00\u4e2a\u6307\u9488\u3001\u5f15\u7528\u6216\u6570\u636e\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u6307\u9488\u3001\u5f15\u7528\u6216\u6570\u636e\u7c7b\u578b\u7684\u503c\uff0c\u9664\u4e86\u65e0\u6cd5\u53bb\u6389const\u5c5e\u6027\uff0c\u53ef\u4ee5\u968f\u4fbf\u8f6c\u6362\uff0c\u4f46\u4e0d\u4f1a\u8fdb\u884c\u4efb\u4f55\u7c7b\u578b\u68c0\u67e5\u6216\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f\uff08RTTI\uff09\u68c0\u67e5\u3002<\/p>\n<pre><code class=\"language-cpp\" lang=\"cpp\">int* ptr = new int(10);\n\/\/ \u5c06 int \u6307\u9488\u8f6c\u6362\u4e3a int \u7c7b\u578b\u7684\u6574\u6570\nint intValue = reinterpret_cast&lt;int&gt;(ptr);\n\n\/\/ \u5c06 int \u7c7b\u578b\u7684\u6574\u6570\u8f6c\u6362\u56de int \u6307\u9488\nint* newPtr = reinterpret_cast&lt;int*&gt;(intValue);\n\n\/\/ \u5c06 int \u6307\u9488\u8f6c\u6362\u4e3a void \u6307\u9488\nvoid* voidPtr = reinterpret_cast&lt;void*&gt;(ptr);\n\n\/\/ \u5c06 void \u6307\u9488\u8f6c\u6362\u56de int \u6307\u9488\nint* anotherPtr = reinterpret_cast&lt;int*&gt;(voidPtr);\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3C++\u4e2d\u7684\u56db\u79cd Cast static_cast static_cast \u662f C++ \u4e2d\u7528\u4e8e\u6267\u884c\u663e\u5f0f\u7c7b\u578b\u8f6c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4],"class_list":["post-389","post","type-post","status-publish","format-standard","hentry","category-c","tag-c"],"_links":{"self":[{"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=389"}],"version-history":[{"count":1,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/389\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/389\/revisions\/390"}],"wp:attachment":[{"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vanforever.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}