{"id":88007,"date":"2026-07-31T10:32:25","date_gmt":"2026-07-31T02:32:25","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/88007.html"},"modified":"2026-07-31T10:32:25","modified_gmt":"2026-07-31T02:32:25","slug":"c-rtti-%e4%b8%8e-dynamic_cast-%e5%ba%95%e5%b1%82%e5%ae%9e%e7%8e%b0%e6%b7%b1%e5%ba%a6%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/88007.html","title":{"rendered":"C++ RTTI \u4e0e dynamic_cast \u5e95\u5c42\u5b9e\u73b0\u6df1\u5ea6\u89e3\u6790"},"content":{"rendered":"<h3 id=\"1-rtti-\u6982\u5ff5\u4e0e\u542f\u7528\u6761\u4ef6\">1. RTTI \u6982\u5ff5\u4e0e\u542f\u7528\u6761\u4ef6<\/h3>\n<h4 id=\"11-\u4ec0\u4e48\u662f-rtti\">1.1 \u4ec0\u4e48\u662f RTTI<\/h4>\n<p>RTTI&#xff08;Run-Time Type Information \/ Runtime Type Identification&#xff09;\u662f C&#043;&#043; \u63d0\u4f9b\u7684\u4e00\u5957\u8fd0\u884c\u65f6\u7c7b\u578b\u67e5\u8be2\u673a\u5236&#xff0c;\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u9636\u6bb5\u83b7\u53d6\u5bf9\u8c61\u7684\u5b9e\u9645\u7c7b\u578b\u4fe1\u606f\u3002RTTI \u662f\u00a0typeid\u00a0\u8fd0\u7b97\u7b26\u548c\u00a0dynamic_cast\u00a0\u8fd0\u7b97\u7b26\u7684\u5e95\u5c42\u652f\u6491\u3002<\/p>\n<p>\u4ece\u7f16\u8bd1\u5668\u5b9e\u73b0\u89d2\u5ea6\u770b&#xff0c;RTTI \u5e76\u975e\u4e00\u4e2a\u72ec\u7acb\u7684\u5b50\u7cfb\u7edf&#xff0c;\u800c\u662f\u7531\u4e09\u90e8\u5206\u534f\u540c\u6784\u6210&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u7ec4\u6210\u90e8\u5206<\/td>\n<td style=\"background-color:#dddddd\">\u4f5c\u7528<\/td>\n<td style=\"background-color:#dddddd\">\u5b58\u50a8\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>std::type_info\u00a0\u5bf9\u8c61<\/td>\n<td>\u5b58\u50a8\u7c7b\u578b\u7684\u54c8\u5e0c\/\u540d\u79f0\u7b49\u9759\u6001\u4fe1\u606f<\/td>\n<td>\u6570\u636e\u6bb5&#xff08;.rodata&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u865a\u8868\u4e2d\u7684 RTTI \u6307\u9488<\/td>\n<td>\u8fde\u63a5\u591a\u6001\u5bf9\u8c61\u4e0e\u5176 type_info<\/td>\n<td>\u865a\u8868\u6761\u76ee<\/td>\n<\/tr>\n<tr>\n<td>\u8fd0\u884c\u65f6\u8f6c\u6362\u7b97\u6cd5<\/td>\n<td>\u904d\u5386\u7c7b\u5c42\u6b21\u7ed3\u6784\u5b8c\u6210\u8f6c\u6362<\/td>\n<td>\u7f16\u8bd1\u5668\u751f\u6210\u7684\u8f85\u52a9\u51fd\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0d\u4e86\u89e3\u865a\u8868\u7684\u53ef\u4ee5\u5148\u5b66\u4e60\u4e00\u4e0b\u524d\u9762\u7684\u5185\u5bb9&#xff1a;C&#043;&#043; \u865a\u51fd\u6570\u8868\u6df1\u5ea6\u5256\u6790&#xff1a;\u4ece\u5355\u7ee7\u627f\u5230\u865a\u62df\u7ee7\u627f\u7684\u5185\u5b58\u5e03\u5c40-CSDN\u535a\u5ba2<\/p>\n<p>\u4e09\u8005\u5173\u7cfb\u5982\u4e0b&#xff1a;<\/p>\n<p> \u591a\u6001\u5bf9\u8c61 \u2192 vptr \u2192 vtable[-1] \u2192 type_info*<br \/>\n                              \u2198 type_info \u5bf9\u8c61\u7684 name() \/ hash_code() \/ before()<\/p>\n<h4 id=\"12-\u542f\u7528\u6761\u4ef6\">1.2 \u542f\u7528\u6761\u4ef6<\/h4>\n<p>RTTI \u4e0d\u662f\u5bf9\u6240\u6709\u7c7b\u578b\u90fd\u53ef\u7528&#xff0c;\u9700\u8981\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u4e4b\u4e00&#xff1a;<\/p>\n<p>\/\/ \u6761\u4ef6\u4e00&#xff1a;\u7c7b\u81f3\u5c11\u6709\u4e00\u4e2a\u865a\u51fd\u6570&#xff08;\u5373\u591a\u6001\u7c7b\u578b&#xff09;<br \/>\nclass Base {<br \/>\npublic:<br \/>\n    virtual ~Base() &#061; default;  \/\/ \u865a\u6790\u6784\u51fd\u6570\u5373\u53ef<br \/>\n};<\/p>\n<p>\/\/ \u6761\u4ef6\u4e8c&#xff1a;\u865a\u7ee7\u627f&#xff08;\u5373\u4f7f\u6ca1\u6709\u865a\u51fd\u6570&#xff0c;\u865a\u57fa\u7c7b\u4e5f\u4f1a\u4ea7\u751f vptr&#xff09;<br \/>\nclass VirtualBase {};<br \/>\nclass Derived : virtual public VirtualBase {};<\/p>\n<p>\/\/ \u6761\u4ef6\u4e09&#xff1a;\u7f16\u8bd1\u5668\u6269\u5c55&#xff08;\u5982 MSVC \u7684 \/GR \u9ed8\u8ba4\u5f00\u542f&#xff09;<br \/>\n\/\/ \u975e\u591a\u6001\u7c7b\u578b\u7684 typeid \u8fd4\u56de\u9759\u6001\u7c7b\u578b&#xff0c;\u4e0d\u6d89\u53ca\u8fd0\u884c\u65f6\u67e5\u8be2<\/p>\n<p>\u5173\u952e\u9650\u5236&#xff1a;\u5bf9\u4e8e\u975e\u591a\u6001\u7c7b\u578b&#xff0c;typeid\u00a0\u8fd4\u56de\u7684\u662f\u7f16\u8bd1\u671f\u9759\u6001\u7c7b\u578b&#xff0c;dynamic_cast\u00a0\u53ea\u80fd\u7528\u4e8e\u591a\u6001\u7c7b\u578b\u7684\u5411\u4e0b\u8f6c\u578b&#xff08;downcast&#xff09;\u3002<\/p>\n<p>struct NonPoly {};<br \/>\nNonPoly np;<br \/>\nconst auto&amp; ti &#061; typeid(np);      \/\/ OK&#xff0c;\u4f46\u8fd4\u56de\u7684\u662f\u9759\u6001\u7c7b\u578b NonPoly<br \/>\n\/\/ dynamic_cast&lt;NonPoly*&gt;(&amp;np);   \/\/ \u7f16\u8bd1\u9519\u8bef&#xff1a;NonPoly \u4e0d\u662f\u591a\u6001\u7c7b\u578b<\/p>\n<h4 id=\"13-\u7f16\u8bd1\u5668\u5f00\u5173\">1.3 \u7f16\u8bd1\u5668\u5f00\u5173<\/h4>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u7f16\u8bd1\u5668<\/td>\n<td style=\"background-color:#dddddd\">\u542f\u7528 RTTI<\/td>\n<td style=\"background-color:#dddddd;width:95px\">\u7981\u7528 RTTI<\/td>\n<td style=\"background-color:#dddddd;width:388px\">\u7981\u7528\u7684\u5f71\u54cd<\/td>\n<\/tr>\n<tr>\n<td>GCC\/Clang<\/td>\n<td>\u9ed8\u8ba4\u5f00\u542f<\/td>\n<td style=\"width:95px\">-fno-rtti<\/td>\n<td style=\"width:388px\">dynamic_cast\u00a0\u548c\u00a0typeid\u00a0\u5bf9\u591a\u6001\u7c7b\u578b\u4e0d\u53ef\u7528&#xff1b;typeid\u00a0\u5bf9\u975e\u591a\u6001\u7c7b\u578b\u4ecd\u53ef\u7528<\/td>\n<\/tr>\n<tr>\n<td>MSVC<\/td>\n<td>\u9ed8\u8ba4\u5f00\u542f&#xff08;\/GR&#xff09;<\/td>\n<td style=\"width:95px\">\/GR-<\/td>\n<td style=\"width:388px\">\u540c\u4e0a&#xff0c;\u4e14\u90e8\u5206\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u80fd\u53d7\u5f71\u54cd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7981\u7528 RTTI \u7684\u5178\u578b\u573a\u666f&#xff1a;\u5d4c\u5165\u5f0f\u7cfb\u7edf\u3001\u6e38\u620f\u5f15\u64ce&#xff08;\u5982 Unreal Engine \u90e8\u5206\u6a21\u5757&#xff09;\u3001\u8ffd\u6c42\u6781\u81f4\u4e8c\u8fdb\u5236\u4f53\u79ef\u7684\u9879\u76ee\u3002\u7981\u7528\u540e\u901a\u5e38\u9700\u8981\u81ea\u5efa\u7c7b\u578b\u8bc6\u522b\u7cfb\u7edf&#xff08;\u5982\u7528\u6574\u578b\u679a\u4e3e ID \u66ff\u4ee3&#xff09;\u3002<\/p>\n<hr \/>\n<h3 id=\"2-type_info-\u7ed3\u6784\u4e0e\u7f16\u8bd1\u5668\u5b9e\u73b0\">2. type_info \u7ed3\u6784\u4e0e\u7f16\u8bd1\u5668\u5b9e\u73b0<\/h3>\n<h4 id=\"21-\u6807\u51c6\u5b9a\u4e49\">2.1 \u6807\u51c6\u5b9a\u4e49<\/h4>\n<p>C&#043;&#043; \u6807\u51c6\u5b9a\u4e49\u4e86\u00a0std::type_info\u00a0\u7c7b\u7684\u63a5\u53e3&#xff08;\u6765\u81ea\u00a0&lt;typeinfo&gt;\u00a0\u5934\u6587\u4ef6&#xff09;&#xff1a;<\/p>\n<p>namespace std {<br \/>\n    class type_info {<br \/>\n    public:<br \/>\n        virtual ~type_info();<br \/>\n        bool operator&#061;&#061;(const type_info&amp; rhs) const noexcept;<br \/>\n        bool operator!&#061;(const type_info&amp; rhs) const noexcept;<br \/>\n        bool before(const type_info&amp; rhs) const noexcept;<br \/>\n        size_t hash_code() const noexcept;<br \/>\n        const char* name() const noexcept;<br \/>\n        type_info(const type_info&amp;) &#061; delete;<br \/>\n        type_info&amp; operator&#061;(const type_info&amp;) &#061; delete;<br \/>\n    };<br \/>\n}<\/p>\n<p>\u63a5\u53e3\u8bbe\u8ba1\u6709\u51e0\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u7981\u6b62\u62f7\u8d1d&#xff1a;type_info\u00a0\u5bf9\u8c61\u662f\u7f16\u8bd1\u5668\u5728\u6570\u636e\u6bb5\u751f\u6210\u7684\u5355\u4f8b&#xff0c;\u6bcf\u4e2a\u7c7b\u578b\u5bf9\u5e94\u552f\u4e00\u5b9e\u4f8b<\/li>\n<li>before()\u00a0\u6392\u5e8f&#xff1a;\u5b9a\u4e49\u4e86\u7c7b\u578b\u95f4\u7684\u5168\u5e8f\u5173\u7cfb&#xff0c;\u7528\u4e8e\u5728\u00a0std::map\u00a0\u7b49\u6709\u5e8f\u5bb9\u5668\u4e2d\u505a\u952e<\/li>\n<li>hash_code()&#xff1a;C&#043;&#043;11 \u5f15\u5165&#xff0c;\u8fd4\u56de\u7c7b\u578b\u7684\u54c8\u5e0c\u503c&#xff0c;\u540c\u4e00\u7c7b\u578b\u8de8\u7ffb\u8bd1\u5355\u5143\u4fdd\u8bc1\u4e00\u81f4<\/li>\n<li>name()\u00a0\u4e0d\u53ef\u79fb\u690d&#xff1a;\u8fd4\u56de\u503c\u662f\u5b9e\u73b0\u5b9a\u4e49\u7684&#xff0c;\u4e0d\u540c\u7f16\u8bd1\u5668\u683c\u5f0f\u5b8c\u5168\u4e0d\u540c<\/li>\n<\/ul>\n<h4 id=\"22-gcc-itanium-abi-\u5b9e\u73b0\">2.2 GCC \/ Itanium ABI \u5b9e\u73b0<\/h4>\n<p>GCC \u548c Clang \u9075\u5faa Itanium C&#043;&#043; ABI\u3002Itanium ABI \u5c06 RTTI \u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u4e3a\u5c42\u6b21\u5316\u7cfb\u7edf&#xff1a;<\/p>\n<p> __type_info (\u57fa\u7c7b)<br \/>\n\u251c\u2500\u2500 __fundamental_type_info   (\u57fa\u672c\u7c7b\u578b: int, double \u7b49)<br \/>\n\u251c\u2500\u2500 __array_type_info         (\u6570\u7ec4\u7c7b\u578b)<br \/>\n\u251c\u2500\u2500 __function_type_info      (\u51fd\u6570\u7c7b\u578b)<br \/>\n\u251c\u2500\u2500 __enum_type_info          (\u679a\u4e3e\u7c7b\u578b)<br \/>\n\u251c\u2500\u2500 __class_type_info         (\u666e\u901a\u7c7b\/\u7ed3\u6784\u4f53)<br \/>\n\u251c\u2500\u2500 __si_class_type_info      (\u5355\u7ee7\u627f\u975e\u865a\u7ee7\u627f\u7c7b)<br \/>\n\u251c\u2500\u2500 __vmi_class_type_info     (\u591a\u7ee7\u627f\u6216\u865a\u7ee7\u627f\u7c7b)<br \/>\n\u2514\u2500\u2500 __pbase_type_info         (\u6307\u9488\u7c7b\u578b&#xff0c;\u542b const\/volatile \u4fee\u9970)<\/p>\n<p>\u5176\u6838\u5fc3\u6570\u636e\u7ed3\u6784&#xff08;\u7b80\u5316\u7248&#xff09;&#xff1a;<\/p>\n<p>\/\/ \u57fa\u7c7b&#xff1a;\u6240\u6709 RTTI \u8282\u70b9\u7684\u57fa\u7840<br \/>\nclass __type_info {<br \/>\nprotected:<br \/>\n    const char* __type_name;  \/\/ \u7ecf\u8fc7\u540d\u79f0\u4fee\u9970&#xff08;name-mangled&#xff09;\u7684\u7c7b\u578b\u540d\u5b57\u7b26\u4e32<br \/>\npublic:<br \/>\n    \/\/ &#8230; operator&#061;&#061; \/ operator!&#061; \u7b49\u6210\u5458<br \/>\n};<\/p>\n<p>\/\/ \u5355\u7ee7\u627f\u7c7b&#xff1a;\u989d\u5916\u5305\u542b\u6307\u5411\u552f\u4e00\u57fa\u7c7b\u7684 RTTI \u6307\u9488<br \/>\nclass __si_class_type_info : public __class_type_info {<br \/>\npublic:<br \/>\n    const __class_type_info* __base_type;  \/\/ \u552f\u4e00\u76f4\u63a5\u57fa\u7c7b\u7684 RTTI<br \/>\n};<\/p>\n<p>\/\/ \u591a\u7ee7\u627f\/\u865a\u7ee7\u627f\u7c7b&#xff1a;\u5305\u542b\u57fa\u7c7b\u8868<br \/>\nclass __vmi_class_type_info : public __class_type_info {<br \/>\npublic:<br \/>\n    unsigned int __flags;                  \/\/ \u6807\u5fd7\u4f4d&#xff08;\u5982\u662f\u5426\u5305\u542b\u865a\u7ee7\u627f&#xff09;<br \/>\n    unsigned int __base_count;             \/\/ \u57fa\u7c7b\u6570\u91cf<br \/>\n    __base_class_type_info __base_info[1]; \/\/ \u53ef\u53d8\u957f\u5ea6\u6570\u7ec4&#xff0c;\u6bcf\u4e2a\u57fa\u7c7b\u7684\u8be6\u7ec6\u4fe1\u606f<br \/>\n};<\/p>\n<p>struct __base_class_type_info {<br \/>\n    const __class_type_info* __base_type;  \/\/ \u57fa\u7c7b\u7684 RTTI \u6307\u9488<br \/>\n    long __offset_flags;                   \/\/ \u504f\u79fb\u91cf\u4e0e\u6807\u5fd7\u4f4d\u6253\u5305\u5b58\u50a8<br \/>\n    \/\/ __offset_flags \u7684\u4f4e 8 \u4f4d\u662f\u6807\u5fd7&#xff1a;<br \/>\n    \/\/   bit 0: __virtual_mask  (\u662f\u5426\u4e3a\u865a\u57fa\u7c7b)<br \/>\n    \/\/   bit 1: __hwm_bit       (\u662f\u5426\u6709\u516c\u5171\u57fa\u7c7b)<br \/>\n    \/\/   \u9ad8\u4f4d: __offset_shift   (\u57fa\u7c7b\u5b50\u5bf9\u8c61\u5728\u6d3e\u751f\u7c7b\u4e2d\u7684\u504f\u79fb\u91cf)<br \/>\n};<\/p>\n<p>__vmi_class_type_info\u00a0\u4e2d\u7684\u00a0__flags\u00a0\u5b57\u6bb5\u533a\u5206\u4ee5\u4e0b\u60c5\u51b5&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">flags \u503c<\/td>\n<td style=\"background-color:#dddddd\">\u542b\u4e49<\/td>\n<td style=\"background-color:#dddddd\">\u4f7f\u7528\u7684 dynamic_cast \u7b97\u6cd5<\/td>\n<\/tr>\n<tr>\n<td>__diamond_shaped_mask<\/td>\n<td>\u94bb\u77f3\u578b\u7ee7\u627f&#xff08;\u542b\u865a\u7ee7\u627f\u7684\u83f1\u5f62&#xff09;<\/td>\n<td>\u9700\u8981\u8bbf\u95ee\u865a\u57fa\u7c7b\u504f\u79fb\u8868<\/td>\n<\/tr>\n<tr>\n<td>__non_diamond_repeat_mask<\/td>\n<td>\u975e\u94bb\u77f3\u578b\u591a\u7ee7\u627f&#xff08;\u91cd\u590d\u57fa\u7c7b\u4f46\u65e0\u865a\u7ee7\u627f&#xff09;<\/td>\n<td>\u6807\u51c6 vmi \u641c\u7d22<\/td>\n<\/tr>\n<tr>\n<td>\u5176\u4ed6<\/td>\n<td>\u666e\u901a\u591a\u7ee7\u627f<\/td>\n<td>\u6807\u51c6 vmi \u641c\u7d22<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"23-msvc-\u5b9e\u73b0\">2.3 MSVC \u5b9e\u73b0<\/h4>\n<p>MSVC \u7684\u5b9e\u73b0\u66f4\u52a0\u7d27\u51d1&#xff0c;RTTI \u4ee5\u00a0RTTICompleteObjectLocator\u00a0\u4e3a\u6838\u5fc3\u7ed3\u6784&#xff1a;<\/p>\n<p> RTTICompleteObjectLocator<br \/>\n\u251c\u2500\u2500 signature (\u9b54\u6570&#xff0c;\u7528\u4e8e\u9a8c\u8bc1)<br \/>\n\u251c\u2500\u2500 offset (this \u8c03\u6574\u91cf)<br \/>\n\u251c\u2500\u2500 cdOffset (\u6784\u9020\u51fd\u6570\u4f4d\u79fb\u504f\u79fb\u91cf)<br \/>\n\u251c\u2500\u2500 pTypeDescriptor \u2192 TypeDescriptor<br \/>\n\u2502   \u251c\u2500\u2500 pVFTable (\u6307\u5411\u865a\u8868\u7684\u6307\u9488)<br \/>\n\u2502   \u251c\u2500\u2500 spare (\u4fdd\u7559)<br \/>\n\u2502   \u2514\u2500\u2500 name (\u7ecf\u8fc7\u4fee\u9970\u7684\u7c7b\u578b\u540d\u79f0\u5b57\u7b26\u4e32)<br \/>\n\u2514\u2500\u2500 pClassDescriptor \u2192 RTTIClassHierarchyDescriptor<br \/>\n    \u251c\u2500\u2500 signature<br \/>\n    \u251c\u2500\u2500 attributes<br \/>\n    \u251c\u2500\u2500 numBaseClasses<br \/>\n    \u2514\u2500\u2500 pBaseClassArray \u2192 RTTIBaseClassDescriptor[]<br \/>\n        \u251c\u2500\u2500 pTypeDescriptor<br \/>\n        \u251c\u2500\u2500 numContainedBases<br \/>\n        \u251c\u2500\u2500 mdisp (\u6210\u5458\u4f4d\u79fb)<br \/>\n        \u251c\u2500\u2500 pdisp (\u865a\u8868\u4f4d\u79fb)<br \/>\n        \u251c\u2500\u2500 vdisp (\u865a\u57fa\u7c7b\u4f4d\u79fb)<br \/>\n        \u2514\u2500\u2500 attributes<\/p>\n<p>MSVC \u7684\u00a0RTTICompleteObjectLocator\u00a0\u5d4c\u5165\u5728\u865a\u8868\u4e2d&#xff08;\u901a\u5e38\u4f4d\u4e8e\u00a0vtable[-1]\u00a0\u4f4d\u7f6e&#xff09;&#xff1a;<\/p>\n<p> \u865a\u8868\u5e03\u5c40&#xff08;MSVC x64&#xff09;&#xff1a;<\/p>\n<p>\u9ad8\u5730\u5740<br \/>\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502   RTTI Complete     \u2502  \u2190 vtable[-1] (offset &#061; -8)<br \/>\n\u2502   Object Locator*   \u2502<br \/>\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524<br \/>\n\u2502   virtual func 1    \u2502  \u2190 vtable[0]<br \/>\n\u2502   virtual func 2    \u2502  \u2190 vtable[1]<br \/>\n\u2502   &#8230;               \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n\u4f4e\u5730\u5740<\/p>\n<h4 id=\"24-name-\u7684\u4e0d\u540c\u7f16\u8bd1\u5668\u8f93\u51fa\">2.4 name() \u7684\u4e0d\u540c\u7f16\u8bd1\u5668\u8f93\u51fa<\/h4>\n<p>type_info::name()\u00a0\u7684\u8f93\u51fa\u662f\u7f16\u8bd1\u5668\u7279\u5b9a\u7684&#xff0c;\u540c\u4e00\u7c7b\u578b\u5728\u4e0d\u540c\u7f16\u8bd1\u5668\u4e0b\u7684\u00a0name()\u00a0\u5b8c\u5168\u4e0d\u540c&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;typeinfo&gt;<\/p>\n<p>class MyClass { virtual ~MyClass() &#061; default; };<\/p>\n<p>int main() {<br \/>\n    MyClass obj;<br \/>\n    std::cout &lt;&lt; typeid(obj).name() &lt;&lt; std::endl;<br \/>\n}<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u7f16\u8bd1\u5668<\/td>\n<td style=\"background-color:#dddddd\">name() \u8f93\u51fa<\/td>\n<td style=\"background-color:#dddddd\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td>GCC\/Linux<\/td>\n<td>7MyClass<\/td>\n<td>Itanium ABI mangled name&#xff08;\u6570\u5b57\u662f\u540d\u79f0\u957f\u5ea6&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>Clang\/Linux<\/td>\n<td>7MyClass<\/td>\n<td>\u4e0e GCC \u517c\u5bb9<\/td>\n<\/tr>\n<tr>\n<td>Clang\/Mac<\/td>\n<td>7MyClass\u00a0\u6216\u00a0MyClass<\/td>\n<td>\u53d6\u51b3\u4e8e\u7248\u672c<\/td>\n<\/tr>\n<tr>\n<td>MSVC<\/td>\n<td>class MyClass<\/td>\n<td>\u4eba\u7c7b\u53ef\u8bfb\u4f46\u65e0\u6807\u51c6\u4fdd\u8bc1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u63d0\u793a&#xff1a;\u82e5\u9700\u8981\u53ef\u79fb\u690d\u7684\u7c7b\u578b\u540d\u79f0&#xff0c;\u53ef\u4f7f\u7528\u00a0boost::core::demangle()\u00a0\u6216 GCC \u7684\u00a0__cxa_demangle()\u00a0\u51fd\u6570\u3002<\/p>\n<hr \/>\n<h3 id=\"3-typeid-\u7684\u5e95\u5c42\u673a\u5236\">3. typeid \u7684\u5e95\u5c42\u673a\u5236<\/h3>\n<h4 id=\"31-\u591a\u6001\u7c7b\u578b\u7684-typeid\">3.1 \u591a\u6001\u7c7b\u578b\u7684 typeid<\/h4>\n<p>\u5f53\u00a0typeid\u00a0\u7684\u64cd\u4f5c\u6570\u662f\u591a\u6001\u7c7b\u578b\u7684\u5f15\u7528\u6216\u89e3\u5f15\u7528\u540e\u7684\u6307\u9488\u65f6&#xff0c;\u7f16\u8bd1\u5668\u751f\u6210\u4ee3\u7801\u6765\u5728\u8fd0\u884c\u65f6\u83b7\u53d6\u5b9e\u9645\u7c7b\u578b&#xff1a;<\/p>\n<p>class Animal { public: virtual ~Animal() &#061; default; };<br \/>\nclass Dog : public Animal {};<\/p>\n<p>Dog dog;<br \/>\nAnimal&amp; ref &#061; dog;<\/p>\n<p>\/\/ \u7f16\u8bd1\u5668\u5c06 typeid(ref) \u8f6c\u6362\u4e3a\u7c7b\u4f3c&#xff1a;<br \/>\n\/\/   const std::type_info&amp; ti &#061; *ref.__vptr[-1].__type_info_ptr;<br \/>\n\/\/ \u672c\u8d28&#xff1a;\u901a\u8fc7\u865a\u8868\u95f4\u63a5\u8bbf\u95ee type_info \u5bf9\u8c61<\/p>\n<p>std::cout &lt;&lt; typeid(ref).name() &lt;&lt; std::endl;  \/\/ \u8f93\u51fa &#034;Dog&#034; \u5bf9\u5e94\u7684\u540d\u79f0<\/p>\n<p>\u5bf9\u5e94\u7684 LLVM IR&#xff08;\u7b80\u5316\u7248&#xff09;\u5927\u81f4\u5982\u4e0b&#xff1a;<\/p>\n<p>; \u52a0\u8f7d vptr<br \/>\n%vptr &#061; load ptr, ptr %ref<br \/>\n; vtable[-1] \u5b58\u50a8 type_info \u6307\u9488&#xff08;Itanium ABI&#xff09;<br \/>\n%ti_ptr_addr &#061; getelementptr ptr, ptr %vptr, i64 -1<br \/>\n%ti_ptr &#061; load ptr, ptr %ti_ptr_addr<br \/>\n; \u8c03\u7528 type_info::name()<br \/>\n%name &#061; call ptr &#064;_ZNKSt9type_info4nameEv(ptr %ti_ptr)<\/p>\n<h4 id=\"32-\u975e\u591a\u6001\u7c7b\u578b\u7684-typeid\">3.2 \u975e\u591a\u6001\u7c7b\u578b\u7684 typeid<\/h4>\n<p>\u5bf9\u4e8e\u975e\u591a\u6001\u7c7b\u578b&#xff0c;typeid\u00a0\u5b8c\u5168\u5728\u7f16\u8bd1\u671f\u89e3\u6790&#xff0c;\u4e0d\u4ea7\u751f\u8fd0\u884c\u65f6\u5f00\u9500&#xff1a;<\/p>\n<p>struct Point { int x, y; };  \/\/ \u975e\u591a\u6001\u7c7b\u578b<\/p>\n<p>Point p;<br \/>\nconst auto&amp; ti &#061; typeid(p);  \/\/ \u7f16\u8bd1\u671f\u89e3\u6790\u4e3a Point \u7684 type_info \u5f15\u7528<br \/>\n\/\/ \u7b49\u4ef7\u4e8e&#xff1a;const auto&amp; ti &#061; typeid(Point);<br \/>\n\/\/ \u4e0d\u7ecf\u8fc7\u865a\u8868&#xff0c;\u76f4\u63a5\u94fe\u63a5\u5230\u6570\u636e\u6bb5\u7684 type_info \u5bf9\u8c61<\/p>\n<p># \u751f\u6210\u7684\u6c47\u7f16&#xff08;x86-64&#xff0c;\u7b80\u5316&#xff09;&#xff1a;<br \/>\nlea     rdi, [rip &#043; typeinfo_for_Point]    # \u76f4\u63a5\u53d6\u5730\u5740&#xff0c;\u96f6\u8fd0\u884c\u65f6\u5f00\u9500<\/p>\n<h4 id=\"33-type_info-\u7684\u76f8\u7b49\u6027\">3.3 type_info \u7684\u76f8\u7b49\u6027<\/h4>\n<p>\u4e24\u4e2a\u00a0type_info\u00a0\u5bf9\u8c61\u76f8\u7b49\u7684\u6761\u4ef6&#xff08;Itanium ABI \u5b9e\u73b0&#xff09;&#xff1a;<\/p>\n<p>bool __type_info::operator&#061;&#061;(const __type_info&amp; rhs) const {<br \/>\n    \/\/ \u7b56\u7565\u4e00&#xff1a;\u5148\u6bd4\u8f83 __type_name \u5b57\u7b26\u4e32\u6307\u9488&#xff08;\u540c\u4e00\u7ffb\u8bd1\u5355\u5143\u5185\u76f8\u540c\u7c7b\u578b\u6307\u5411\u540c\u4e00\u5b57\u7b26\u4e32&#xff09;<br \/>\n    if (__type_name &#061;&#061; rhs.__type_name) return true;<br \/>\n    \/\/ \u7b56\u7565\u4e8c&#xff1a;\u6307\u9488\u4e0d\u540c\u65f6&#xff0c;\u6bd4\u8f83\u5b57\u7b26\u4e32\u5185\u5bb9&#xff08;\u8de8\u52a8\u6001\u5e93\u573a\u666f&#xff09;<br \/>\n    \/\/ \u6ce8&#xff1a;\u90e8\u5206\u7f16\u8bd1\u5668\u5b9e\u73b0\u4ec5\u6bd4\u8f83\u6307\u9488&#xff0c;\u8de8 .so \u65f6\u53ef\u80fd\u8bef\u5224 false<br \/>\n    return __type_name[0] &#061;&#061; &#039;*&#039; ? false : strcmp(__type_name, rhs.__type_name) &#061;&#061; 0;<br \/>\n}<\/p>\n<p>\u5e38\u89c1\u9677\u9631&#xff1a;\u5728 Linux \u4e0a&#xff0c;\u5f53\u540c\u4e00\u4e2a\u52a8\u6001\u5e93\u88ab\u00a0dlopen\u00a0\u591a\u6b21\u52a0\u8f7d&#xff08;\u5982\u00a0RTLD_LOCAL\u00a0\u6a21\u5f0f&#xff09;\u65f6&#xff0c;\u540c\u4e00\u7c7b\u578b\u7684\u00a0type_info\u00a0\u53ef\u80fd\u88ab\u590d\u5236\u591a\u4efd&#xff0c;\u5bfc\u81f4\u00a0typeid(A) &#061;&#061; typeid(A)\u00a0\u8fd4\u56de\u00a0false\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u786e\u4fdd\u4f7f\u7528\u00a0RTLD_GLOBAL\u00a0\u6216\u8ba9\u52a8\u6001\u5e93\u94fe\u63a5\u65f6\u4f7f\u7528\u00a0-Wl,-Bsymbolic\u3002<\/p>\n<hr \/>\n<h3 id=\"4-dynamic_cast-\u7684\u4e09\u79cd\u5b9e\u73b0\u7b56\u7565\">4. dynamic_cast \u7684\u4e09\u79cd\u5b9e\u73b0\u7b56\u7565<\/h3>\n<p>dynamic_cast\u00a0\u7684\u6838\u5fc3\u6311\u6218\u662f&#xff1a;\u7ed9\u5b9a\u4e00\u4e2a\u57fa\u7c7b\u6307\u9488\u548c\u76ee\u6807\u7c7b\u578b&#xff0c;\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u8be5\u6307\u9488\u5b9e\u9645\u6307\u5411\u7684\u5bf9\u8c61\u662f\u5426\u80fd\u5b89\u5168\u5730\u8f6c\u6362\u4e3a\u76ee\u6807\u7c7b\u578b&#xff0c;\u5e76\u5728\u591a\u7ee7\u627f\/\u865a\u7ee7\u627f\u4e0b\u6b63\u786e\u8c03\u6574\u6307\u9488\u503c\u3002<\/p>\n<p>\u4e3b\u6d41\u7f16\u8bd1\u5668\u91c7\u7528\u4ee5\u4e0b\u4e09\u79cd\u7b56\u7565\u4e4b\u4e00&#xff1a;<\/p>\n<h4 id=\"41-\u7b56\u7565\u5bf9\u6bd4\u603b\u89c8\">4.1 \u7b56\u7565\u5bf9\u6bd4\u603b\u89c8<\/h4>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u7b56\u7565<\/td>\n<td style=\"background-color:#dddddd;width:113px\">\u4ee3\u8868\u7f16\u8bd1\u5668<\/td>\n<td style=\"background-color:#dddddd;width:103px\">\u65f6\u95f4\u590d\u6742\u5ea6<\/td>\n<td style=\"background-color:#dddddd;width:82px\">\u7a7a\u95f4\u5f00\u9500<\/td>\n<td style=\"background-color:#dddddd;width:223px\">\u6838\u5fc3\u601d\u60f3<\/td>\n<\/tr>\n<tr>\n<td>\u865a\u8868\u904d\u5386&#xff08;Vtable Walk&#xff09;<\/td>\n<td style=\"width:113px\">GCC\/Clang&#xff08;\u975e\u865a\u7ee7\u627f\u8def\u5f84&#xff09;<\/td>\n<td style=\"width:103px\">O(N) N&#061;\u7ee7\u627f\u6df1\u5ea6<\/td>\n<td style=\"width:82px\">\u4f4e<\/td>\n<td style=\"width:223px\">\u6cbf vtable \u4e2d\u7684 RTTI \u94fe\u5411\u4e0a\u904d\u5386&#xff0c;\u9010\u4e2a\u6bd4\u8f83 type_info<\/td>\n<\/tr>\n<tr>\n<td>\u7c7b\u578b\u6811\u641c\u7d22&#xff08;Type Tree Search&#xff09;<\/td>\n<td style=\"width:113px\">GCC\/Clang&#xff08;\u865a\u7ee7\u627f\u8def\u5f84&#xff09;<\/td>\n<td style=\"width:103px\">O(B) B&#061;\u57fa\u7c7b\u6570\u91cf<\/td>\n<td style=\"width:82px\">\u4e2d<\/td>\n<td style=\"width:223px\">\u9012\u5f52\u904d\u5386\u00a0__vmi_class_type_info\u00a0\u4e2d\u7684\u57fa\u7c7b\u6570\u7ec4<\/td>\n<\/tr>\n<tr>\n<td>\u5d4c\u5165\u54c8\u5e0c\u8868&#xff08;Embedded Hash&#xff09;<\/td>\n<td style=\"width:113px\">MSVC<\/td>\n<td style=\"width:103px\">O(1) ~ O(N)<\/td>\n<td style=\"width:82px\">\u9ad8<\/td>\n<td style=\"width:223px\">\u5728\u865a\u8868\u9644\u8fd1\u9884\u8ba1\u7b97\u5b8c\u6574\u7ee7\u627f\u94fe\u7684 type_info \u54c8\u5e0c\u8868<\/td>\n<\/tr>\n<tr>\n<td>\u5b57\u7b26\u4e32\u6bd4\u8f83&#xff08;String Comparison&#xff09;<\/td>\n<td style=\"width:113px\">\u67d0\u4e9b\u5d4c\u5165\u5f0f\u7f16\u8bd1\u5668<\/td>\n<td style=\"width:103px\">O(1) \u4f46\u5e38\u6570\u5927<\/td>\n<td style=\"width:82px\">\u4f4e<\/td>\n<td style=\"width:223px\">\u76f4\u63a5\u7528\u5b57\u7b26\u4e32\u6bd4\u8f83\u4ee3\u66ff type_info \u6307\u9488\u6bd4\u8f83<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"42-\u7b56\u7565\u4e00\u865a\u8868\u904d\u5386gccclang-\u5355\u7ee7\u627f\u8def\u5f84\">4.2 \u7b56\u7565\u4e00&#xff1a;\u865a\u8868\u904d\u5386&#xff08;GCC\/Clang \u5355\u7ee7\u627f\u8def\u5f84&#xff09;<\/h4>\n<p>\u8fd9\u662f\u6700\u7b80\u5355\u4e5f\u6700\u5feb\u7684\u7b56\u7565&#xff0c;\u9002\u7528\u4e8e\u5355\u7ee7\u627f\u94fe&#xff1a;<\/p>\n<p> dynamic_cast&lt;Dog*&gt;(animal_ptr) \u7684\u6267\u884c\u6d41\u7a0b&#xff1a;<\/p>\n<p>1. \u89e3\u5f15\u7528 animal_ptr \u83b7\u53d6 vptr<br \/>\n2. \u8bfb\u53d6 vtable[-1] \u83b7\u53d6 type_info*&#xff08;\u5f53\u524d\u5bf9\u8c61\u7684\u5b9e\u9645\u7c7b\u578b&#xff09;<br \/>\n3. \u6bd4\u8f83\u8be5 type_info \u4e0e\u76ee\u6807\u7c7b\u578b Dog \u7684 type_info<br \/>\n   &#8211; \u76f8\u7b49 \u2192 \u8f6c\u6362\u6210\u529f&#xff0c;\u76f4\u63a5\u8fd4\u56de\u6307\u9488\u503c&#xff08;\u5355\u7ee7\u627f\u65e0\u9700\u8c03\u6574 this&#xff09;<br \/>\n   &#8211; \u4e0d\u76f8\u7b49 \u2192 \u901a\u8fc7 __si_class_type_info::__base_type \u8df3\u5230\u7236\u7c7b\u7684 type_info&#xff0c;\u91cd\u590d\u6b65\u9aa4 3<br \/>\n4. \u5230\u8fbe\u7ee7\u627f\u94fe\u9876\u7aef\u4ecd\u672a\u5339\u914d \u2192 \u8fd4\u56de nullptr<\/p>\n<p>\u7b97\u6cd5\u4f2a\u4ee3\u7801&#xff1a;<\/p>\n<p>void* __dynamic_cast_si(const void* obj_ptr,<br \/>\n                        const __class_type_info* obj_type,<br \/>\n                        const __class_type_info* target_type,<br \/>\n                        ptrdiff_t src2dst_offset) {<br \/>\n    const void* vptr &#061; *(const void**)obj_ptr;<br \/>\n    const auto* rtti_ptr &#061; ((const __class_type_info**)vptr)[-1];<\/p>\n<p>    \/\/ \u6cbf\u5355\u7ee7\u627f\u94fe\u5411\u4e0a\u904d\u5386<br \/>\n    for (const __si_class_type_info* cur &#061; static_cast&lt;const __si_class_type_info*&gt;(rtti_ptr);<br \/>\n         cur !&#061; nullptr;<br \/>\n         cur &#061; dynamic_cast&lt;const __si_class_type_info*&gt;(cur-&gt;__base_type)) {<\/p>\n<p>        if (cur &#061;&#061; target_type) {<br \/>\n            \/\/ \u627e\u5230\u76ee\u6807\u7c7b\u578b&#xff0c;\u8c03\u6574\u6307\u9488<br \/>\n            return (char*)obj_ptr &#043; src2dst_offset;<br \/>\n        }<\/p>\n<p>        \/\/ \u5982\u679c\u5f53\u524d\u5df2\u4e0d\u662f\u5355\u7ee7\u627f\u7c7b\u578b&#xff0c;\u8df3\u51fa\u5faa\u73af\u8d70\u590d\u6742\u8def\u5f84<br \/>\n        if (typeid(*cur) !&#061; typeid(__si_class_type_info)) break;<br \/>\n    }<\/p>\n<p>    \/\/ \u964d\u7ea7\u5230\u901a\u7528\u641c\u7d22\u7b97\u6cd5<br \/>\n    return __dynamic_cast_generic(obj_ptr, obj_type, target_type, src2dst_offset);<br \/>\n}<\/p>\n<h4 id=\"43-\u7b56\u7565\u4e8c\u7c7b\u578b\u6811\u641c\u7d22gccclang-\u865a\u7ee7\u627f\u8def\u5f84\">4.3 \u7b56\u7565\u4e8c&#xff1a;\u7c7b\u578b\u6811\u641c\u7d22&#xff08;GCC\/Clang \u865a\u7ee7\u627f\u8def\u5f84&#xff09;<\/h4>\n<p>\u5f53\u7c7b\u5c42\u6b21\u7ed3\u6784\u6d89\u53ca\u591a\u7ee7\u627f\u6216\u865a\u7ee7\u627f\u65f6&#xff0c;RTTI \u7ed3\u6784\u4ece\u00a0__si_class_type_info\u00a0\u53d8\u4e3a\u00a0__vmi_class_type_info&#xff0c;\u9700\u8981\u904d\u5386\u57fa\u7c7b\u6570\u7ec4&#xff1a;<\/p>\n<p>\/\/ \u901a\u7528\u591a\u7ee7\u627f dynamic_cast \u6838\u5fc3\u7b97\u6cd5<br \/>\nvoid* __dynamic_cast_generic(const void* obj_ptr,<br \/>\n                              const __class_type_info* obj_type,<br \/>\n                              const __class_type_info* target_type,<br \/>\n                              ptrdiff_t src2dst) {<br \/>\n    \/\/ \u6b65\u9aa4 1&#xff1a;\u68c0\u67e5\u5f53\u524d\u5c42\u7ea7\u662f\u5426\u5339\u914d<br \/>\n    if (*obj_type &#061;&#061; *target_type) {<br \/>\n        return (char*)obj_ptr &#043; src2dst;<br \/>\n    }<\/p>\n<p>    \/\/ \u6b65\u9aa4 2&#xff1a;\u6839\u636e obj_type \u7684\u5177\u4f53\u7c7b\u578b\u9012\u5f52\u641c\u7d22<br \/>\n    \/\/ \u5982\u679c\u662f\u5355\u7ee7\u627f\u7c7b\u578b<br \/>\n    if (const auto* si &#061; dynamic_cast&lt;const __si_class_type_info*&gt;(obj_type)) {<br \/>\n        return __dynamic_cast_generic(obj_ptr, si-&gt;__base_type, target_type, src2dst);<br \/>\n    }<\/p>\n<p>    \/\/ \u5982\u679c\u662f\u591a\u7ee7\u627f\/\u865a\u7ee7\u627f\u7c7b\u578b<br \/>\n    if (const auto* vmi &#061; dynamic_cast&lt;const __vmi_class_type_info*&gt;(obj_type)) {<br \/>\n        for (unsigned i &#061; 0; i &lt; vmi-&gt;__base_count; &#043;&#043;i) {<br \/>\n            const auto&amp; base &#061; vmi-&gt;__base_info[i];<\/p>\n<p>            \/\/ \u63d0\u53d6\u504f\u79fb\u91cf\u548c\u6807\u5fd7\u4f4d<br \/>\n            bool is_virtual &#061; base.__offset_flags &amp; 1;<br \/>\n            ptrdiff_t offset;<\/p>\n<p>            if (is_virtual) {<br \/>\n                \/\/ \u865a\u57fa\u7c7b&#xff1a;\u504f\u79fb\u91cf\u9700\u8981\u901a\u8fc7\u865a\u57fa\u7c7b\u8868\u5728\u8fd0\u884c\u65f6\u786e\u5b9a<br \/>\n                offset &#061; get_virtual_base_offset(obj_ptr, base);<br \/>\n            } else {<br \/>\n                \/\/ \u975e\u865a\u57fa\u7c7b&#xff1a;\u504f\u79fb\u91cf\u7f16\u7801\u5728 __offset_flags \u4e2d<br \/>\n                offset &#061; base.__offset_flags &gt;&gt; 8;<br \/>\n            }<\/p>\n<p>            \/\/ \u9012\u5f52\u641c\u7d22\u5f53\u524d\u57fa\u7c7b\u5b50\u6811<br \/>\n            void* result &#061; __dynamic_cast_generic(<br \/>\n                (char*)obj_ptr &#043; offset,<br \/>\n                base.__base_type,<br \/>\n                target_type,<br \/>\n                src2dst &#8211; offset<br \/>\n            );<\/p>\n<p>            if (result) return result;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return nullptr;  \/\/ \u672a\u627e\u5230\u76ee\u6807\u7c7b\u578b<br \/>\n}<\/p>\n<h4 id=\"44-\u7b56\u7565\u4e09msvc-\u7684\u5d4c\u5165\u54c8\u5e0c\u8868\">4.4 \u7b56\u7565\u4e09&#xff1a;MSVC \u7684\u5d4c\u5165\u54c8\u5e0c\u8868<\/h4>\n<p>MSVC \u91c7\u7528\u4e0e GCC \u5b8c\u5168\u4e0d\u540c\u7684\u7b56\u7565\u2014\u2014\u5728\u00a0RTTIClassHierarchyDescriptor\u00a0\u4e2d\u9884\u8ba1\u7b97\u5b8c\u6574\u7684\u57fa\u7c7b\u94fe&#xff0c;\u4ee5\u6570\u7ec4\u5f62\u5f0f\u5b58\u50a8&#xff0c;\u901a\u8fc7\u904d\u5386\u8be5\u6570\u7ec4\u5b9e\u73b0 O(N) \u67e5\u627e&#xff1a;<\/p>\n<p>struct RTTIClassHierarchyDescriptor {<br \/>\n    unsigned signature;<br \/>\n    unsigned attributes;    \/\/ \u7ee7\u627f\u5c5e\u6027\u6807\u5fd7<br \/>\n    unsigned numBaseClasses; \/\/ \u57fa\u7c7b\u6570\u91cf&#xff08;\u542b\u81ea\u8eab&#xff09;<br \/>\n    RTTIBaseClassDescriptor** pBaseClassArray;<br \/>\n};<\/p>\n<p>struct RTTIBaseClassDescriptor {<br \/>\n    TypeDescriptor* pTypeDescriptor;  \/\/ \u6307\u5411 type_info \u7b49\u4ef7\u7ed3\u6784<br \/>\n    unsigned numContainedBases;       \/\/ \u8be5\u57fa\u7c7b\u5305\u542b\u7684\u5b50\u57fa\u7c7b\u6570\u91cf<br \/>\n    PMD where;                        \/\/ \u6307\u9488\u6210\u5458\u4f4d\u79fb&#xff08;Pointer-to-Member Displacement&#xff09;<br \/>\n    unsigned attributes;              \/\/ \u5c5e\u6027\u6807\u5fd7<br \/>\n};<\/p>\n<p>struct PMD {<br \/>\n    ptrdiff_t mdisp;  \/\/ \u6210\u5458\u4f4d\u79fb&#xff08;member displacement&#xff09;<br \/>\n    ptrdiff_t pdisp;  \/\/ \u865a\u8868\u4f4d\u79fb&#xff08;vtable displacement&#xff09;<br \/>\n    ptrdiff_t vdisp;  \/\/ \u865a\u57fa\u7c7b\u4f4d\u79fb&#xff08;virtual base displacement&#xff09;<br \/>\n};<\/p>\n<p>MSVC \u7684\u00a0dynamic_cast\u00a0\u6838\u5fc3\u6d41\u7a0b&#xff1a;<\/p>\n<p>1. \u4ece obj \u83b7\u53d6 vptr 2. \u8bfb\u53d6 vtable[-1] \u83b7\u53d6 RTTICompleteObjectLocator* 3. \u904d\u5386 ClassHierarchyDescriptor \u4e2d\u7684 pBaseClassArray \u00a0\u00a0 &#8211; \u5bf9\u6bcf\u4e2a\u5143\u7d20&#xff0c;\u6bd4\u8f83 pTypeDescriptor \u4e0e\u76ee\u6807\u7c7b\u578b\u7684 TypeDescriptor \u00a0\u00a0 &#8211; \u5339\u914d\u6210\u529f\u65f6&#xff0c;\u4f7f\u7528 PMD \u4e2d\u7684 mdisp\/pdisp\/vdisp \u8ba1\u7b97\u6b63\u786e\u7684\u6307\u9488\u503c 4. \u904d\u5386\u5b8c\u4ecd\u672a\u5339\u914d \u2192 nullptr<\/p>\n<p>MSVC \u8fd9\u79cd&#034;\u9884\u8ba1\u7b97\u6570\u7ec4&#034;\u65b9\u5f0f\u4f7f\u5f97\u6bcf\u6b21\u00a0dynamic_cast\u00a0\u90fd\u662f\u7ebf\u6027\u626b\u63cf&#xff0c;\u4f46\u4e0d\u9700\u8981\u50cf GCC \u90a3\u6837\u9012\u5f52\u6df1\u5165\u7ee7\u627f\u6811\u3002\u5728\u6df1\u5c42\u7ee7\u627f\u65f6 MSVC \u53ef\u80fd\u66f4\u5feb&#xff08;\u6570\u7ec4\u8fde\u7eed\u8bbf\u95ee vs \u6307\u9488\u8df3\u8f6c&#xff09;&#xff0c;\u4f46\u591a\u7ee7\u627f\u4e0b\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a O(B)\u3002<\/p>\n<hr \/>\n<h3 id=\"5-\u5355\u7ee7\u627f\u4e0b\u7684-dynamic_cast\">5. \u5355\u7ee7\u627f\u4e0b\u7684 dynamic_cast<\/h3>\n<h4 id=\"51-\u57fa\u672c\u5411\u4e0a\u8f6c\u578bupcast\">5.1 \u57fa\u672c\u5411\u4e0a\u8f6c\u578b&#xff08;Upcast&#xff09;<\/h4>\n<p>\u5411\u4e0a\u8f6c\u578b&#xff08;\u6d3e\u751f\u7c7b\u6307\u9488\u2192\u57fa\u7c7b\u6307\u9488&#xff09;\u5728\u7f16\u8bd1\u671f\u5b8c\u6210&#xff0c;\u4e0d\u6d89\u53ca RTTI&#xff1a;<\/p>\n<p>class Animal { public: virtual ~Animal() &#061; default; };<br \/>\nclass Dog : public Animal {};<\/p>\n<p>Dog* dog &#061; new Dog;<br \/>\nAnimal* animal &#061; dog;  \/\/ \u7f16\u8bd1\u671f\u9690\u5f0f\u8f6c\u6362&#xff0c;\u65e0\u9700 dynamic_cast<br \/>\n\/\/ \u7b49\u4ef7\u4e8e&#xff1a;Animal* animal &#061; static_cast&lt;Animal*&gt;(dog);<\/p>\n<h4 id=\"52-\u5411\u4e0b\u8f6c\u578bdowncast\">5.2 \u5411\u4e0b\u8f6c\u578b&#xff08;Downcast&#xff09;<\/h4>\n<p>Animal* animal &#061; new Dog;<\/p>\n<p>\/\/ dynamic_cast \u5728\u8fd0\u884c\u65f6\u9a8c\u8bc1 animal \u662f\u5426\u771f\u7684\u6307\u5411 Dog<br \/>\nDog* dog &#061; dynamic_cast&lt;Dog*&gt;(animal);<br \/>\nif (dog) {<br \/>\n    dog-&gt;bark();<br \/>\n}<\/p>\n<p>\u5355\u7ee7\u627f\u4e0b\u7684\u00a0dynamic_cast&lt;Dog*&gt;\u00a0\u5b9e\u73b0\u975e\u5e38\u76f4\u63a5&#xff1a;<\/p>\n<p> \u5185\u5b58\u5e03\u5c40&#xff08;\u5355\u7ee7\u627f&#xff09;&#xff1a;<br \/>\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502  Animal vptr \u2502 \u2190 animal \u548c dog \u6307\u5411\u540c\u4e00\u5730\u5740<br \/>\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524<br \/>\n\u2502  Dog members \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>\u8f6c\u6362\u8fc7\u7a0b&#xff1a;<br \/>\n1. \u901a\u8fc7 vptr \u83b7\u53d6\u5b9e\u9645\u7c7b\u578b\u7684 type_info&#xff08;\u6b64\u5904\u4e3a Dog&#xff09;<br \/>\n2. \u6cbf RTTI \u94fe\u5411\u4e0a\u67e5\u627e&#xff1a;Dog \u2192 Animal<br \/>\n3. \u53d1\u73b0 Dog &#061;&#061; \u76ee\u6807\u7c7b\u578b Dog&#xff0c;\u5339\u914d\u6210\u529f<br \/>\n4. \u6307\u9488\u65e0\u9700\u8c03\u6574&#xff08;\u57fa\u7c7b\u5b50\u5bf9\u8c61\u4e0e\u6d3e\u751f\u7c7b\u8d77\u59cb\u5730\u5740\u4e00\u81f4&#xff09;<\/p>\n<h4 id=\"53-\u4ea4\u53c9\u8f6c\u578bcross-cast\">5.3 \u4ea4\u53c9\u8f6c\u578b&#xff08;Cross-cast&#xff09;<\/h4>\n<p>\u4ea4\u53c9\u8f6c\u578b\u6307\u5728\u540c\u4e00\u7ee7\u627f\u5c42\u7ea7\u4e2d\u4e0d\u540c\u5206\u652f\u95f4\u7684\u8f6c\u6362&#xff0c;\u5355\u7ee7\u627f\u4e0b\u4e0d\u6d89\u53ca\u6b64\u573a\u666f&#xff08;\u9700\u8981\u591a\u7ee7\u627f&#xff09;&#xff0c;\u4f46\u7f16\u8bd1\u671f\u4e0d\u4f1a\u62a5\u9519&#xff1a;<\/p>\n<p>class Base { public: virtual ~Base() &#061; default; };<br \/>\nclass A : public Base {};<br \/>\nclass B : public Base {};<\/p>\n<p>Base* base &#061; new A;<br \/>\nB* b &#061; dynamic_cast&lt;B*&gt;(base);  \/\/ \u8fd4\u56de nullptr&#xff08;A \u4e0d\u662f B&#xff09;<\/p>\n<hr \/>\n<h3 id=\"6-\u591a\u7ee7\u627f\u4e0b\u7684\u6307\u9488\u8c03\u6574\">6. \u591a\u7ee7\u627f\u4e0b\u7684\u6307\u9488\u8c03\u6574<\/h3>\n<p>\u591a\u7ee7\u627f\u662f\u00a0dynamic_cast\u00a0\u6700\u590d\u6742\u7684\u573a\u666f\u4e4b\u4e00\u3002\u6d3e\u751f\u7c7b\u5305\u542b\u591a\u4e2a\u57fa\u7c7b\u5b50\u5bf9\u8c61&#xff0c;\u5b83\u4eec\u4f4d\u4e8e\u4e0d\u540c\u7684\u5185\u5b58\u504f\u79fb\u5904\u3002<\/p>\n<h4 id=\"61-\u5185\u5b58\u5e03\u5c40\u793a\u4f8b\">6.1 \u5185\u5b58\u5e03\u5c40\u793a\u4f8b<\/h4>\n<p>class Animal {<br \/>\npublic:<br \/>\n    virtual void eat() {}<br \/>\n    virtual ~Animal() &#061; default;<br \/>\n    int age &#061; 0;<br \/>\n};<\/p>\n<p>class Flyable {<br \/>\npublic:<br \/>\n    virtual void fly() {}<br \/>\n    virtual ~Flyable() &#061; default;<br \/>\n    double wingspan &#061; 0.0;<br \/>\n};<\/p>\n<p>class Bat : public Animal, public Flyable {<br \/>\npublic:<br \/>\n    void eat() override {}<br \/>\n    void fly() override {}<br \/>\n    bool echolocation &#061; true;<br \/>\n};<\/p>\n<p>\u5178\u578b\u5185\u5b58\u5e03\u5c40&#xff08;GCC\/Clang x86-64&#xff09;&#xff1a;<\/p>\n<p> Bat \u5bf9\u8c61\u5185\u5b58\u5e03\u5c40&#xff1a;<\/p>\n<p>\u504f\u79fb\u91cf    \u5185\u5bb9<br \/>\n&#043;0    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n      \u2502  Animal vptr \u2502 \u2500\u2500\u2192 Bat-Animal \u865a\u8868<br \/>\n&#043;8    \u2502  age &#061; 0     \u2502<br \/>\n&#043;16   \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524<br \/>\n      \u2502  Flyable vptr\u2502 \u2500\u2500\u2192 Bat-Flyable \u865a\u8868&#xff08;\u542b this \u8c03\u6574 thunk&#xff09;<br \/>\n&#043;24   \u2502  wingspan&#061;0.0\u2502<br \/>\n&#043;32   \u2502  echolocation\u2502<br \/>\n      \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>\u6ce8&#xff1a;Animal \u5b50\u5bf9\u8c61\u4ece\u504f\u79fb 0 \u5f00\u59cb<br \/>\n     Flyable \u5b50\u5bf9\u8c61\u4ece\u504f\u79fb 16 \u5f00\u59cb<\/p>\n<h4 id=\"62-\u6307\u9488\u8c03\u6574\u7684\u6838\u5fc3\u95ee\u9898\">6.2 \u6307\u9488\u8c03\u6574\u7684\u6838\u5fc3\u95ee\u9898<\/h4>\n<p>\u5173\u952e\u95ee\u9898&#xff1a;\u5f53\u6301\u6709\u00a0Animal*\u00a0\u6307\u9488\u4f46\u5b9e\u9645\u4e0a\u6307\u5411\u00a0Bat\u00a0\u5bf9\u8c61\u65f6&#xff0c;\u5982\u4f55\u5c06\u5176\u5b89\u5168\u5730\u8f6c\u6362\u4e3a\u00a0Flyable*&#xff1f;<\/p>\n<p> Animal* a &#061; new Bat;   \/\/ a \u6307\u5411 Bat \u5bf9\u8c61\u7684\u504f\u79fb 0&#xff08;Animal \u5b50\u5bf9\u8c61\u8d77\u59cb&#xff09;<br \/>\nFlyable* f &#061; dynamic_cast&lt;Flyable*&gt;(a);<\/p>\n<p>\/\/ \u95ee\u9898&#xff1a;a \u6307\u5411\u5730\u5740 0x1000&#xff0c;\u4f46 Flyable \u5b50\u5bf9\u8c61\u5728 0x1000 &#043; 16 &#061; 0x1010<br \/>\n\/\/ dynamic_cast \u5fc5\u987b\u5c06\u8fd4\u56de\u503c\u8c03\u6574\u4e3a 0x1010&#xff0c;\u800c\u4e0d\u7b80\u5355\u8fd4\u56de 0x1000<\/p>\n<h4 id=\"63-\u7f16\u8bd1\u5668\u5982\u4f55\u8ba1\u7b97\u8c03\u6574\u91cf\">6.3 \u7f16\u8bd1\u5668\u5982\u4f55\u8ba1\u7b97\u8c03\u6574\u91cf<\/h4>\n<p>dynamic_cast\u00a0\u7684\u6307\u9488\u8c03\u6574\u901a\u8fc7 RTTI \u6570\u636e\u7ed3\u6784\u4e2d\u5b58\u50a8\u7684\u504f\u79fb\u4fe1\u606f\u6765\u5b9e\u73b0&#xff1a;<\/p>\n<p>GCC\/Itanium ABI \u65b9\u5f0f&#xff1a;<\/p>\n<p>struct __base_class_type_info {<br \/>\n    const __class_type_info* __base_type;<br \/>\n    long __offset_flags;  \/\/ \u504f\u79fb\u91cf\u7f16\u7801&#xff1a;<br \/>\n                          \/\/ bits [0:7]   &#061; \u6807\u5fd7\u4f4d<br \/>\n                          \/\/ bits [8:63]  &#061; \u504f\u79fb\u91cf&#xff08;\u53f3\u79fb 8 \u4f4d&#xff09;<br \/>\n};<\/p>\n<p>\u5728\u00a0__vmi_class_type_info\u00a0\u7684\u00a0__base_info\u00a0\u6570\u7ec4\u4e2d&#xff0c;\u6bcf\u4e2a\u57fa\u7c7b\u90fd\u8bb0\u5f55\u4e86\u5b83\u5728\u6d3e\u751f\u7c7b\u5bf9\u8c61\u4e2d\u7684\u504f\u79fb\u91cf\u3002<\/p>\n<p>\u5f53\u00a0dynamic_cast\u00a0\u904d\u5386\u57fa\u7c7b\u6570\u7ec4\u65f6&#xff0c;\u6bcf\u6b21\u9012\u5f52\u8c03\u7528\u90fd\u4f1a\u4f20\u9012\u4e00\u4e2a\u7d2f\u79ef\u504f\u79fb\u91cf\u00a0src2dst&#xff1a;<\/p>\n<p>\/\/ \u521d\u59cb\u8c03\u7528&#xff1a;src2dst &#061; 0&#xff08;\u4ece Animal* \u7684\u8d77\u59cb\u4f4d\u7f6e\u8ba1\u7b97&#xff09;<br \/>\n\/\/ \u904d\u5386\u5230 Flyable \u57fa\u7c7b\u6761\u76ee\u65f6&#xff0c;\u53d1\u73b0 offset &#061; 16<br \/>\n\/\/ \u9012\u5f52\u8c03\u7528\u65f6\u4f20\u9012 src2dst &#061; 0 &#8211; 16 &#061; -16<br \/>\n\/\/ \u5728 Flyable \u7684 type_info \u8282\u70b9\u5339\u914d\u65f6&#xff1a;<br \/>\n\/\/   \u8fd4\u56de (char*)obj_ptr &#043; (-16) &#061; obj_ptr &#8211; 16&#xff0c;\u5373 Flyable \u5b50\u5bf9\u8c61\u7684\u5730\u5740<\/p>\n<p>MSVC \u65b9\u5f0f&#xff1a;<\/p>\n<p>MSVC \u4f7f\u7528\u00a0PMD&#xff08;Pointer-to-Member Displacement&#xff09;\u7ed3\u6784&#xff0c;\u5728\u00a0RTTIBaseClassDescriptor\u00a0\u4e2d\u9884\u5148\u5b58\u50a8\u4e86\u5b8c\u6574\u7684\u8c03\u6574\u53c2\u6570&#xff1a;<\/p>\n<p>struct PMD {<br \/>\n    ptrdiff_t mdisp;  \/\/ \u6210\u5458\u4f4d\u79fb&#xff1a;\u4ece\u6d3e\u751f\u7c7b\u8d77\u59cb\u5230\u8be5\u57fa\u7c7b\u5b50\u5bf9\u8c61\u7684\u504f\u79fb<br \/>\n    ptrdiff_t pdisp;  \/\/ \u865a\u8868\u4f4d\u79fb&#xff1a;\u865a\u8868\u5185\u8c03\u6574<br \/>\n    ptrdiff_t vdisp;  \/\/ \u865a\u57fa\u7c7b\u4f4d\u79fb&#xff1a;\u4ec5\u865a\u7ee7\u627f\u4e0b\u975e\u96f6<br \/>\n};<\/p>\n<h4 id=\"64-\u5b8c\u6574\u8f6c\u6362\u793a\u4f8b\">6.4 \u5b8c\u6574\u8f6c\u6362\u793a\u4f8b<\/h4>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;typeinfo&gt;<\/p>\n<p>class Animal {<br \/>\npublic:<br \/>\n    virtual const char* type() { return &#034;Animal&#034;; }<br \/>\n    virtual ~Animal() &#061; default;<br \/>\n};<\/p>\n<p>class Flyable {<br \/>\npublic:<br \/>\n    virtual const char* ability() { return &#034;Flyable&#034;; }<br \/>\n    virtual ~Flyable() &#061; default;<br \/>\n};<\/p>\n<p>class Bat : public Animal, public Flyable {<br \/>\npublic:<br \/>\n    const char* type() override { return &#034;Bat&#034;; }<br \/>\n    const char* ability() override { return &#034;Bat-Flyable&#034;; }<br \/>\n};<\/p>\n<p>int main() {<br \/>\n    Bat bat;<br \/>\n    Animal* a &#061; &amp;bat;          \/\/ a \u6307\u5411\u504f\u79fb 0<br \/>\n    Flyable* f &#061; &amp;bat;         \/\/ f \u6307\u5411\u504f\u79fb 16&#xff08;\u5047\u8bbe&#xff09;<\/p>\n<p>    std::cout &lt;&lt; &#034;a &#061; &#034; &lt;&lt; a &lt;&lt; std::endl;<br \/>\n    std::cout &lt;&lt; &#034;f &#061; &#034; &lt;&lt; f &lt;&lt; std::endl;<br \/>\n    std::cout &lt;&lt; &#034;&amp;bat &#061; &#034; &lt;&lt; &amp;bat &lt;&lt; std::endl;<br \/>\n    \/\/ \u8f93\u51fa&#xff1a;a &#061;&#061; &amp;bat, f &#061;&#061; &amp;bat &#043; offset_of_Flyable<\/p>\n<p>    \/\/ \u4ea4\u53c9\u8f6c\u578b&#xff1a;\u4ece Animal* \u8f6c\u5230 Flyable*<br \/>\n    Flyable* cross &#061; dynamic_cast&lt;Flyable*&gt;(a);<br \/>\n    std::cout &lt;&lt; &#034;cross &#061; &#034; &lt;&lt; cross &lt;&lt; std::endl;<br \/>\n    \/\/ cross \u5e94\u8be5\u7b49\u4e8e f&#xff0c;\u5373\u6307\u9488\u503c\u7ecf\u8fc7\u8c03\u6574<\/p>\n<p>    \/\/ \u65e0\u6548\u8f6c\u578b<br \/>\n    class Unrelated { public: virtual ~Unrelated() &#061; default; };<br \/>\n    Unrelated* u &#061; dynamic_cast&lt;Unrelated*&gt;(a);<br \/>\n    std::cout &lt;&lt; &#034;u &#061; &#034; &lt;&lt; u &lt;&lt; std::endl;  \/\/ nullptr<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4 id=\"65-\u5b50\u5bf9\u8c61\u504f\u79fb\u91cf\u5b9e\u9a8c\">6.5 \u5b50\u5bf9\u8c61\u504f\u79fb\u91cf\u5b9e\u9a8c<\/h4>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u89c2\u5bdf\u4e0d\u540c\u57fa\u7c7b\u6307\u9488\u5bf9\u5e94\u5730\u5740\u7684\u5dee\u5f02&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>class A {<br \/>\n    virtual ~A() &#061; default;<br \/>\n    char data_a[8];<br \/>\n};<br \/>\nclass B {<br \/>\n    virtual ~B() &#061; default;<br \/>\n    char data_b[8];<br \/>\n};<br \/>\nclass C : public A, public B {<br \/>\n    char data_c[8];<br \/>\n};<\/p>\n<p>int main() {<br \/>\n    C c;<\/p>\n<p>    A* pa &#061; &amp;c;<br \/>\n    B* pb &#061; &amp;c;<br \/>\n    C* pc &#061; &amp;c;<\/p>\n<p>    std::cout &lt;&lt; &#034;C*  &#061; &#034; &lt;&lt; pc &lt;&lt; std::endl;<br \/>\n    std::cout &lt;&lt; &#034;A*  &#061; &#034; &lt;&lt; pa &lt;&lt; &#034;  (offset &#061; &#034;<br \/>\n              &lt;&lt; (char*)pa &#8211; (char*)pc &lt;&lt; &#034;)&#034; &lt;&lt; std::endl;<br \/>\n    std::cout &lt;&lt; &#034;B*  &#061; &#034; &lt;&lt; pb &lt;&lt; &#034;  (offset &#061; &#034;<br \/>\n              &lt;&lt; (char*)pb &#8211; (char*)pc &lt;&lt; &#034;)&#034; &lt;&lt; std::endl;<\/p>\n<p>    \/\/ \u9a8c\u8bc1 dynamic_cast \u6307\u9488\u8c03\u6574<br \/>\n    B* pb2 &#061; dynamic_cast&lt;B*&gt;(pa);<br \/>\n    std::cout &lt;&lt; &#034;dynamic_cast&lt;B*&gt;(A*) &#061; &#034; &lt;&lt; pb2 &lt;&lt; std::endl;<br \/>\n    std::cout &lt;&lt; &#034;pb &#061;&#061; pb2: &#034; &lt;&lt; (pb &#061;&#061; pb2) &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>\u5178\u578b\u8f93\u51fa&#xff08;x86-64&#xff09;&#xff1a;<\/p>\n<p> C*  &#061; 0x7ffe12345000<br \/>\nA*  &#061; 0x7ffe12345000  (offset &#061; 0)<br \/>\nB*  &#061; 0x7ffe12345010  (offset &#061; 16)<br \/>\ndynamic_cast&lt;B*&gt;(A*) &#061; 0x7ffe12345010<br \/>\npb &#061;&#061; pb2: 1<\/p>\n<hr \/>\n<h3 id=\"7-\u865a\u7ee7\u627f\u4e0b\u7684-dynamic_cast\">7. \u865a\u7ee7\u627f\u4e0b\u7684 dynamic_cast<\/h3>\n<p>\u865a\u7ee7\u627f\u5f15\u5165\u4e86\u4e00\u4e2a\u989d\u5916\u7684\u95f4\u63a5\u5c42&#xff0c;\u4f7f\u5f97\u00a0dynamic_cast\u00a0\u7684\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u3002<\/p>\n<h4 id=\"71-\u865a\u7ee7\u627f\u7684\u5185\u5b58\u5e03\u5c40\">7.1 \u865a\u7ee7\u627f\u7684\u5185\u5b58\u5e03\u5c40<\/h4>\n<p>class Animal {<br \/>\npublic:<br \/>\n    virtual void eat() {}<br \/>\n    int animal_id &#061; 0;<br \/>\n};<\/p>\n<p>class Mammal : virtual public Animal {<br \/>\npublic:<br \/>\n    virtual void nurse() {}<br \/>\n    int fur_type &#061; 0;<br \/>\n};<\/p>\n<p>class Bird : virtual public Animal {<br \/>\npublic:<br \/>\n    virtual void lay_eggs() {}<br \/>\n    int feather_color &#061; 0;<br \/>\n};<\/p>\n<p>class Platypus : public Mammal, public Bird {<br \/>\npublic:<br \/>\n    void eat() override {}<br \/>\n    bool venomous &#061; true;<br \/>\n};<\/p>\n<p>\u5178\u578b\u5185\u5b58\u5e03\u5c40&#xff08;GCC x86-64&#xff0c;\u7b80\u5316\u8868\u793a&#xff09;&#xff1a;<\/p>\n<p> Platypus \u5bf9\u8c61\u5185\u5b58\u5e03\u5c40&#xff1a;<\/p>\n<p>&#043;0    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n      \u2502  Mammal vptr      \u2502 \u2500\u2500\u2192 Mammal-in-Platypus \u865a\u8868<br \/>\n&#043;8    \u2502  fur_type         \u2502<br \/>\n&#043;16   \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524<br \/>\n      \u2502  Bird vptr        \u2502 \u2500\u2500\u2192 Bird-in-Platypus \u865a\u8868<br \/>\n&#043;24   \u2502  feather_color    \u2502<br \/>\n&#043;32   \u2502  venomous         \u2502<br \/>\n&#043;40   \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524<br \/>\n      \u2502  Animal vptr      \u2502 \u2500\u2500\u2192 Animal-in-Platypus \u865a\u8868&#xff08;\u865a\u57fa\u7c7b&#xff0c;\u4ec5\u4e00\u4efd&#xff09;<br \/>\n&#043;48   \u2502  animal_id        \u2502<br \/>\n      \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>\u5173\u952e\u70b9&#xff1a;<br \/>\n&#8211; Animal \u53ea\u6709\u4e00\u4efd&#xff08;\u865a\u7ee7\u627f\u4fdd\u8bc1&#xff09;<br \/>\n&#8211; Mammal \u548c Bird \u90fd\u901a\u8fc7\u865a\u57fa\u7c7b\u8868\u95f4\u63a5\u8bbf\u95ee Animal<br \/>\n&#8211; Animal \u7684\u504f\u79fb\u91cf\u5728 Mammal \u548c Bird \u4e2d\u4e0d\u4e00\u5b9a\u76f8\u540c<\/p>\n<h4 id=\"72-\u865a\u57fa\u7c7b\u504f\u79fb\u7684\u8fd0\u884c\u65f6\u89e3\u6790\">7.2 \u865a\u57fa\u7c7b\u504f\u79fb\u7684\u8fd0\u884c\u65f6\u89e3\u6790<\/h4>\n<p>\u865a\u7ee7\u627f\u4e0b&#xff0c;\u57fa\u7c7b\u5b50\u5bf9\u8c61\u7684\u504f\u79fb\u91cf\u4e0d\u80fd\u5728\u7f16\u8bd1\u671f\u786e\u5b9a&#xff08;\u56e0\u4e3a\u540c\u4e00\u4e2a\u7c7b\u53ef\u80fd\u88ab\u653e\u5728\u4e0d\u540c\u6d3e\u751f\u5c42\u6b21\u7684\u5bf9\u8c61\u4e2d&#xff09;\u3002\u504f\u79fb\u91cf\u5b58\u50a8\u5728\u865a\u57fa\u7c7b\u8868&#xff08;vbase offset table&#xff09;\u4e2d&#xff0c;\u901a\u8fc7\u865a\u8868\u95f4\u63a5\u8bbf\u95ee\u3002<\/p>\n<p>GCC \u7684\u5b9e\u73b0\u65b9\u5f0f&#xff1a;<\/p>\n<p> Mammal \u865a\u8868\u5e03\u5c40&#xff08;\u7b80\u5316&#xff09;&#xff1a;<br \/>\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502  vbase_offset &#061; 40        \u2502 \u2190 \u4ece Mammal \u5b50\u5bf9\u8c61\u8d77\u59cb\u5230\u865a\u57fa\u7c7b Animal \u7684\u504f\u79fb<br \/>\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524<br \/>\n\u2502  vcall_offset (\u53ef\u9009\u9879)    \u2502<br \/>\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524<br \/>\n\u2502  type_info* (RTTI)        \u2502<br \/>\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524<br \/>\n\u2502  Mammal::nurse()          \u2502<br \/>\n\u2502  Mammal \u5176\u4ed6\u865a\u51fd\u6570        \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>\u8fd0\u884c\u65f6\u83b7\u53d6\u865a\u57fa\u7c7b\u504f\u79fb\u7684\u4f2a\u4ee3\u7801&#xff1a;<\/p>\n<p>ptrdiff_t get_virtual_base_offset(const void* obj_ptr,<br \/>\n                                   const __base_class_type_info&amp; base) {<br \/>\n    \/\/ \u865a\u57fa\u7c7b\u7684\u504f\u79fb\u5b58\u50a8\u5728\u5f53\u524d\u5bf9\u8c61\u865a\u8868\u4e2d\u7684\u7279\u5b9a\u4f4d\u7f6e<br \/>\n    const void* vptr &#061; *(const void**)obj_ptr;<\/p>\n<p>    \/\/ \u4ece RTTI \u7684 __offset_flags \u4e2d\u63d0\u53d6\u865a\u57fa\u7c7b\u8868\u7684\u7d22\u5f15<br \/>\n    \/\/ \u865a\u57fa\u7c7b\u504f\u79fb\u5728\u865a\u8868\u4e2d\u7684\u4f4d\u7f6e\u76f8\u5bf9\u4e8e vptr<br \/>\n    ptrdiff_t vbase_offset_index &#061; extract_vbase_index(base.__offset_flags);<\/p>\n<p>    \/\/ \u865a\u57fa\u7c7b\u504f\u79fb\u5b58\u50a8\u5728 vptr \u4e4b\u524d\u7684\u67d0\u4e2a\u4f4d\u7f6e&#xff08;\u5b9e\u73b0\u5b9a\u4e49&#xff09;<br \/>\n    const ptrdiff_t* vbase_offsets &#061; ((const ptrdiff_t*)vptr) &#8211; vbase_offset_index &#8211; 1;<\/p>\n<p>    return *vbase_offsets;<br \/>\n}<\/p>\n<h4 id=\"73-\u865a\u7ee7\u627f\u4e0b\u7684-dynamic_cast-\u5b8c\u6574\u6d41\u7a0b\">7.3 \u865a\u7ee7\u627f\u4e0b\u7684 dynamic_cast \u5b8c\u6574\u6d41\u7a0b<\/h4>\n<p>class Animal { public: virtual ~Animal() &#061; default; };<br \/>\nclass Mammal : virtual public Animal {};<br \/>\nclass Bird : virtual public Animal {};<br \/>\nclass Platypus : public Mammal, public Bird {};<\/p>\n<p>Platypus duckbill;<br \/>\nAnimal* a &#061; &amp;duckbill;  \/\/ a \u6307\u5411\u865a\u57fa\u7c7b Animal<\/p>\n<p>\/\/ \u4ece\u865a\u57fa\u7c7b Animal* \u5411\u4e0b\u8f6c\u578b\u4e3a Platypus*<br \/>\nPlatypus* p &#061; dynamic_cast&lt;Platypus*&gt;(a);<\/p>\n<p>\/\/ \u6d41\u7a0b&#xff1a;<br \/>\n\/\/ 1. \u4ece a-&gt;vptr \u83b7\u53d6 type_info&#xff08;\u5b9e\u9645\u7c7b\u578b\u4e3a Platypus&#xff09;<br \/>\n\/\/ 2. Platypus \u7684 RTTI \u7c7b\u578b\u4e3a __vmi_class_type_info<br \/>\n\/\/ 3. \u904d\u5386\u57fa\u7c7b\u6570\u7ec4&#xff08;Mammal \u548c Bird&#xff09;<br \/>\n\/\/    &#8211; Mammal \u662f\u666e\u901a\u57fa\u7c7b&#xff0c;\u901a\u8fc7\u5b83\u9012\u5f52\u8fdb\u5165 Animal&#xff08;\u865a\u57fa\u7c7b&#xff09;<br \/>\n\/\/    &#8211; \u5728 Animal \u5c42\u7ea7\u5339\u914d\u65f6&#xff0c;\u9700\u8981\u8c03\u6574 src2dst \u7d2f\u52a0\u504f\u79fb<br \/>\n\/\/ 4. \u4ece Animal \u4f4d\u7f6e\u51cf\u53bb Animal \u5b50\u5bf9\u8c61\u5728 Platypus \u4e2d\u7684\u504f\u79fb\u91cf<br \/>\n\/\/    \u8be5\u504f\u79fb\u91cf\u901a\u8fc7 Mammal \u7684\u865a\u57fa\u7c7b\u8868\u5728\u8fd0\u884c\u65f6\u786e\u5b9a<\/p>\n<hr \/>\n<h3 id=\"8-\u8de8\u5c42\u7ea7\u8f6c\u6362\u4e0e\u6b67\u4e49\u68c0\u6d4b\">8. \u8de8\u5c42\u7ea7\u8f6c\u6362\u4e0e\u6b67\u4e49\u68c0\u6d4b<\/h3>\n<h4 id=\"81-\u8de8\u5c42\u7ea7\u8f6c\u6362\">8.1 \u8de8\u5c42\u7ea7\u8f6c\u6362<\/h4>\n<p>dynamic_cast\u00a0\u652f\u6301\u8df3\u8fc7\u4e2d\u95f4\u5c42\u7ea7\u7684\u8f6c\u6362&#xff1a;<\/p>\n<p>class A { public: virtual ~A() &#061; default; };<br \/>\nclass B : public A {};<br \/>\nclass C : public B {};<\/p>\n<p>A* a &#061; new C;<br \/>\nC* c &#061; dynamic_cast&lt;C*&gt;(a);  \/\/ \u76f4\u63a5\u4ece A* \u8f6c\u5230 C*&#xff0c;\u8df3\u8fc7 B<\/p>\n<p>\/\/ \u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u6cbf\u7ee7\u627f\u94fe\u67e5\u627e C&#xff0c;\u65e0\u9700\u6bcf\u5c42\u624b\u52a8\u8f6c\u6362<\/p>\n<p>\u8fd9\u5728\u83f1\u5f62\u7ee7\u627f\u4e2d\u5c24\u5176\u91cd\u8981&#xff1a;<\/p>\n<p>class Top { public: virtual ~Top() &#061; default; };<br \/>\nclass Left : virtual public Top {};<br \/>\nclass Right : virtual public Top {};<br \/>\nclass Bottom : public Left, public Right {};<\/p>\n<p>Bottom b;<br \/>\nTop* t &#061; &amp;b;<\/p>\n<p>\/\/ \u76f4\u63a5\u4ece Top* \u8f6c\u5230 Bottom*&#xff0c;\u8de8\u8d8a Left \u548c Right<br \/>\nBottom* bt &#061; dynamic_cast&lt;Bottom*&gt;(t);<br \/>\n\/\/ \u7f16\u8bd1\u5668\u5fc5\u987b\u901a\u8fc7 Top \u7684 type_info \u627e\u5230 Bottom&#xff0c;\u5e76\u8ba1\u7b97\u6b63\u786e\u7684\u504f\u79fb\u91cf<\/p>\n<h4 id=\"82-\u6b67\u4e49\u68c0\u6d4b\">8.2 \u6b67\u4e49\u68c0\u6d4b<\/h4>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b&#xff0c;dynamic_cast\u00a0\u53ef\u80fd\u4f1a\u9047\u5230\u6b67\u4e49&#xff08;\u975e\u865a\u7ee7\u627f\u83f1\u5f62&#xff09;&#xff1a;<\/p>\n<p>class Top { public: virtual ~Top() &#061; default; };<br \/>\nclass Left : public Top {};    \/\/ \u975e\u865a\u7ee7\u627f<br \/>\nclass Right : public Top {};   \/\/ \u975e\u865a\u7ee7\u627f<br \/>\nclass Bottom : public Left, public Right {};<\/p>\n<p>Bottom b;<br \/>\nTop* t_left &#061; static_cast&lt;Left*&gt;(&amp;b);   \/\/ \u660e\u786e\u6307\u5411 Left \u4e2d\u7684 Top<br \/>\nTop* t_right &#061; static_cast&lt;Right*&gt;(&amp;b); \/\/ \u660e\u786e\u6307\u5411 Right \u4e2d\u7684 Top<\/p>\n<p>\/\/ \u4ece Top* \u8f6c\u5230 Bottom* \u2014\u2014 \u53ef\u80fd\u4ea7\u751f\u6b67\u4e49<br \/>\nBottom* bt &#061; dynamic_cast&lt;Bottom*&gt;(t_left);<br \/>\n\/\/ GCC \u884c\u4e3a&#xff1a;\u6210\u529f\u8f6c\u6362&#xff08;\u56e0\u4e3a\u53ef\u4ee5\u901a\u8fc7 RTTI \u8def\u5f84\u53cd\u63a8&#xff09;<br \/>\n\/\/ \u4f46\u5982\u679c Top \u6709\u591a\u4e2a\u5b9e\u4f8b\u4e14\u65e0\u6cd5\u552f\u4e00\u786e\u5b9a\u8def\u5f84&#xff0c;\u53ef\u80fd\u5931\u8d25<\/p>\n<p>\/\/ \u66f4\u660e\u786e\u7684\u6b67\u4e49\u573a\u666f&#xff1a;\u4ece\u865a\u57fa\u7c7b\u6307\u9488\u540c\u65f6\u5339\u914d\u4e24\u6761\u8def\u5f84<br \/>\n\/\/ \u5f53\u7f16\u8bd1\u5668\u65e0\u6cd5\u786e\u5b9a\u552f\u4e00\u8def\u5f84\u65f6&#xff0c;dynamic_cast \u8fd4\u56de nullptr<\/p>\n<h4 id=\"83-\u6b67\u4e49\u7684\u8fd0\u884c\u65f6\u68c0\u6d4b\">8.3 \u6b67\u4e49\u7684\u8fd0\u884c\u65f6\u68c0\u6d4b<\/h4>\n<p>GCC \u7684\u00a0__dynamic_cast\u00a0\u5b9e\u73b0\u4f1a\u68c0\u6d4b\u6b67\u4e49&#xff1a;<\/p>\n<p>\/\/ \u7b80\u5316\u7248\u6b67\u4e49\u68c0\u6d4b\u903b\u8f91<br \/>\nvoid* __dynamic_cast_verify(const void* obj_ptr,<br \/>\n                             const __class_type_info* obj_type,<br \/>\n                             const __class_type_info* target_type) {<br \/>\n    void* result &#061; nullptr;<br \/>\n    int match_count &#061; 0;<\/p>\n<p>    \/\/ \u9012\u5f52\u641c\u7d22\u6240\u6709\u57fa\u7c7b\u8def\u5f84<br \/>\n    __dynamic_cast_search(obj_ptr, obj_type, target_type, 0, result, match_count);<\/p>\n<p>    \/\/ \u627e\u5230\u591a\u6761\u8def\u5f84 \u2192 \u6b67\u4e49<br \/>\n    if (match_count &gt; 1) {<br \/>\n        return nullptr;<br \/>\n    }<\/p>\n<p>    return result;<br \/>\n}<\/p>\n<hr \/>\n<h3 id=\"9-\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5\">9. \u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5<\/h3>\n<h4 id=\"91-\u6d4b\u8bd5\u4ee3\u7801\">9.1 \u6d4b\u8bd5\u4ee3\u7801<\/h4>\n<p>#include &lt;chrono&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<\/p>\n<p>\/\/ \u6d4b\u8bd5\u7528\u7c7b\u5c42\u6b21<br \/>\nclass Base0 {<br \/>\npublic:<br \/>\n    virtual ~Base0() &#061; default;<br \/>\n    virtual int f0() { return 0; }<br \/>\n    char padding0[64];  \/\/ \u6a21\u62df\u771f\u5b9e\u5bf9\u8c61\u5927\u5c0f<br \/>\n};<\/p>\n<p>class Base1 {<br \/>\npublic:<br \/>\n    virtual ~Base1() &#061; default;<br \/>\n    virtual int f1() { return 1; }<br \/>\n    char padding1[64];<br \/>\n};<\/p>\n<p>class Base2 {<br \/>\npublic:<br \/>\n    virtual ~Base2() &#061; default;<br \/>\n    virtual int f2() { return 2; }<br \/>\n    char padding2[64];<br \/>\n};<\/p>\n<p>class DerivedSI : public Base0 {<br \/>\npublic:<br \/>\n    int val;<br \/>\n};<\/p>\n<p>class DerivedMI : public Base0, public Base1, public Base2 {<br \/>\npublic:<br \/>\n    int val;<br \/>\n};<\/p>\n<p>class VirtualBase : virtual public Base0 {};<\/p>\n<p>class DerivedVI : public VirtualBase, public Base1 {<br \/>\npublic:<br \/>\n    int val;<br \/>\n};<\/p>\n<p>template&lt;typename From, typename To&gt;<br \/>\ndouble benchmark_dynamic_cast(size_t iterations) {<br \/>\n    From* ptr &#061; new To;<br \/>\n    ptr-&gt;val &#061; 42;<\/p>\n<p>    auto start &#061; std::chrono::high_resolution_clock::now();<br \/>\n    for (size_t i &#061; 0; i &lt; iterations; &#043;&#043;i) {<br \/>\n        To* result &#061; dynamic_cast&lt;To*&gt;(ptr);<br \/>\n        \/\/ \u9632\u6b62\u4f18\u5316\u5668\u79fb\u9664\u8c03\u7528<br \/>\n        if (result) result-&gt;val &#061; i;<br \/>\n    }<br \/>\n    auto end &#061; std::chrono::high_resolution_clock::now();<\/p>\n<p>    delete ptr;<br \/>\n    return std::chrono::duration&lt;double, std::nano&gt;(end &#8211; start).count() \/ iterations;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    constexpr size_t N &#061; 10&#039;000&#039;000;<\/p>\n<p>    std::cout &lt;&lt; &#034;dynamic_cast \u5e73\u5747\u8017\u65f6 (ns\/\u6b21, &#034; &lt;&lt; N &lt;&lt; &#034; \u6b21\u6d4b\u91cf):\\\\n&#034;;<br \/>\n    std::cout &lt;&lt; &#034;  \u5355\u7ee7\u627f Base0* \u2192 DerivedSI*:    &#034;<br \/>\n              &lt;&lt; benchmark_dynamic_cast&lt;Base0, DerivedSI&gt;(N) &lt;&lt; &#034; ns\\\\n&#034;;<br \/>\n    std::cout &lt;&lt; &#034;  \u591a\u7ee7\u627f Base0* \u2192 DerivedMI*:    &#034;<br \/>\n              &lt;&lt; benchmark_dynamic_cast&lt;Base0, DerivedMI&gt;(N) &lt;&lt; &#034; ns\\\\n&#034;;<br \/>\n    std::cout &lt;&lt; &#034;  \u865a\u7ee7\u627f Base0* \u2192 DerivedVI*:    &#034;<br \/>\n              &lt;&lt; benchmark_dynamic_cast&lt;Base0, DerivedVI&gt;(N) &lt;&lt; &#034; ns\\\\n&#034;;<br \/>\n}<\/p>\n<h4 id=\"92-\u5178\u578b\u6d4b\u8bd5\u7ed3\u679cgcc-13-o2-x86-64\">9.2 \u5178\u578b\u6d4b\u8bd5\u7ed3\u679c&#xff08;GCC 13, -O2, x86-64&#xff09;<\/h4>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u8f6c\u6362\u573a\u666f<\/td>\n<td style=\"background-color:#dddddd\">\u5e73\u5747\u8017\u65f6<\/td>\n<td style=\"background-color:#dddddd\">\u76f8\u5bf9\u5355\u7ee7\u627f\u500d\u6570<\/td>\n<\/tr>\n<tr>\n<td>\u5355\u7ee7\u627f Base0* \u2192 DerivedSI*<\/td>\n<td>~4 ns<\/td>\n<td>1.0x&#xff08;\u57fa\u7ebf&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u7ee7\u627f Base0* \u2192 DerivedMI*&#xff08;\u540c\u504f\u79fb\u57fa\u7c7b&#xff09;<\/td>\n<td>~5 ns<\/td>\n<td>1.25x<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u7ee7\u627f Base0* \u2192 DerivedMI*&#xff08;\u8de8\u57fa\u7c7b&#xff0c;\u9700\u8c03\u6574\u6307\u9488&#xff09;<\/td>\n<td>~8 ns<\/td>\n<td>2.0x<\/td>\n<\/tr>\n<tr>\n<td>\u865a\u7ee7\u627f Base0* \u2192 DerivedVI*<\/td>\n<td>~25 ns<\/td>\n<td>6.25x<\/td>\n<\/tr>\n<tr>\n<td>\u65e0\u6548\u8f6c\u6362&#xff08;\u8fd4\u56de nullptr&#xff09;<\/td>\n<td>~3 ns<\/td>\n<td>0.75x<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"93-\u6027\u80fd\u5f71\u54cd\u56e0\u7d20\u6392\u540d\">9.3 \u6027\u80fd\u5f71\u54cd\u56e0\u7d20\u6392\u540d<\/h4>\n<li>\u865a\u7ee7\u627f&#xff1a;\u6700\u5927\u7684\u6027\u80fd\u6740\u624b&#xff08;5-10x \u5f00\u9500&#xff09;&#xff0c;\u9700\u8981\u8fd0\u884c\u65f6\u67e5\u8be2\u865a\u57fa\u7c7b\u504f\u79fb\u8868<\/li>\n<li>\u7ee7\u627f\u6df1\u5ea6&#xff1a;\u6bcf\u589e\u52a0\u4e00\u5c42\u7ee7\u627f\u7ea6\u589e\u52a0 2-3 ns&#xff08;GCC \u5355\u7ee7\u627f\u94fe\u904d\u5386&#xff09;<\/li>\n<li>\u57fa\u7c7b\u6570\u91cf&#xff1a;\u591a\u7ee7\u627f\u4e0b\u57fa\u7c7b\u6570\u7ec4\u904d\u5386&#xff0c;\u6bcf\u4e2a\u57fa\u7c7b\u6761\u76ee\u589e\u52a0\u5c11\u91cf\u5f00\u9500<\/li>\n<li>\u7f13\u5b58\u5c40\u90e8\u6027&#xff1a;\u865a\u8868\u3001RTTI \u7ed3\u6784\u548c type_info \u5b57\u7b26\u4e32\u7684\u7f13\u5b58\u547d\u4e2d\u7387<\/li>\n<hr \/>\n<h3 id=\"10-\u7f16\u8bd1\u5668\u5dee\u5f02\u5bf9\u6bd4\">10. \u7f16\u8bd1\u5668\u5dee\u5f02\u5bf9\u6bd4<\/h3>\n<h4 id=\"101-\u6838\u5fc3\u5dee\u5f02\u6c47\u603b\">10.1 \u6838\u5fc3\u5dee\u5f02\u6c47\u603b<\/h4>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u7279\u6027<\/td>\n<td style=\"background-color:#dddddd\">GCC (Itanium ABI)<\/td>\n<td style=\"background-color:#dddddd\">MSVC<\/td>\n<td style=\"background-color:#dddddd\">Clang<\/td>\n<\/tr>\n<tr>\n<td>RTTI \u7ed3\u6784<\/td>\n<td>\u5c42\u6b21\u5316 type_info \u6d3e\u751f\u7c7b<\/td>\n<td>\u6241\u5e73\u5316\u7684 ClassHierarchyDescriptor<\/td>\n<td>\u4e0e GCC \u517c\u5bb9&#xff08;Itanium ABI&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>type_info \u5d4c\u5165\u4f4d\u7f6e<\/td>\n<td>vtable[-1]&#xff08;\u865a\u8868\u524d\u4e00\u4e2a\u69fd\u4f4d&#xff09;<\/td>\n<td>vtable[-1]&#xff08;RTTICompleteObjectLocator*&#xff09;<\/td>\n<td>\u540c GCC<\/td>\n<\/tr>\n<tr>\n<td>dynamic_cast \u6838\u5fc3\u7b97\u6cd5<\/td>\n<td>\u9012\u5f52\u904d\u5386 RTTI \u6811<\/td>\n<td>\u7ebf\u6027\u626b\u63cf\u57fa\u7c7b\u63cf\u8ff0\u7b26\u6570\u7ec4<\/td>\n<td>\u540c GCC&#xff0c;\u90e8\u5206\u8def\u5f84\u6709\u4f18\u5316<\/td>\n<\/tr>\n<tr>\n<td>\u5355\u7ee7\u627f\u5904\u7406<\/td>\n<td>O(depth) \u865a\u8868\u94fe\u904d\u5386<\/td>\n<td>O(N) \u626b\u63cf&#xff08;N&#061;\u57fa\u7c7b\u6570\u91cf&#xff09;<\/td>\n<td>\u540c GCC<\/td>\n<\/tr>\n<tr>\n<td>\u865a\u7ee7\u627f\u5904\u7406<\/td>\n<td>\u9012\u5f52\u641c\u7d22 vmi type_info<\/td>\n<td>PMD.vdisp &#043; \u865a\u57fa\u7c7b\u8868<\/td>\n<td>\u540c GCC<\/td>\n<\/tr>\n<tr>\n<td>\u7a7a\u6307\u9488\u884c\u4e3a<\/td>\n<td>dynamic_cast&lt;T*&gt;(nullptr)\u00a0\u2192 nullptr<\/td>\n<td>\u540c\u5de6<\/td>\n<td>\u540c\u5de6<\/td>\n<\/tr>\n<tr>\n<td>\u7981\u7528 RTTI<\/td>\n<td>-fno-rtti<\/td>\n<td>\/GR-<\/td>\n<td>-fno-rtti<\/td>\n<\/tr>\n<tr>\n<td>type_info::name() \u683c\u5f0f<\/td>\n<td>\u540d\u79f0\u4fee\u9970\u5b57\u7b26\u4e32&#xff08;7MyClass&#xff09;<\/td>\n<td>\u4eba\u7c7b\u53ef\u8bfb&#xff08;class MyClass&#xff09;<\/td>\n<td>\u540c GCC<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"102-gccclang-\u7684\u4f18\u5316\u6280\u5de7\">10.2 GCC\/Clang \u7684\u4f18\u5316\u6280\u5de7<\/h4>\n<p>GCC \u5bf9\u67d0\u4e9b\u5e38\u89c1\u6a21\u5f0f\u505a\u4e86\u7279\u6b8a\u4f18\u5316&#xff1a;<\/p>\n<p>\/\/ \u6a21\u5f0f 1&#xff1a;\u76ee\u6807\u7c7b\u578b\u662f final \u7c7b<br \/>\nclass Derived final : public Base {};<\/p>\n<p>Base* b &#061; new Derived;<br \/>\n\/\/ \u5982\u679c\u7f16\u8bd1\u5668\u80fd\u9759\u6001\u63a8\u65ad b \u6307\u5411 Derived&#xff0c;\u53ef\u80fd\u76f4\u63a5\u751f\u6210&#xff1a;<br \/>\n\/\/   Derived* d &#061; static_cast&lt;Derived*&gt;(b);  \/\/ \u8df3\u8fc7 RTTI \u5f00\u9500<\/p>\n<p>\/\/ \u6a21\u5f0f 2&#xff1a;\u5df2\u77e5\u6210\u529f\u8def\u5f84<br \/>\n\/\/ \u5f53\u7f16\u8bd1\u5668\u901a\u8fc7\u5206\u6790\u786e\u5b9a\u8f6c\u6362\u5fc5\u7136\u6210\u529f\u65f6&#xff0c;\u53ef\u80fd\u7701\u7565\u8fd0\u884c\u65f6\u68c0\u67e5<\/p>\n<h4 id=\"103-\u8de8\u7f16\u8bd1\u5668\u517c\u5bb9\u7684\u6ce8\u610f\u4e8b\u9879\">10.3 \u8de8\u7f16\u8bd1\u5668\u517c\u5bb9\u7684\u6ce8\u610f\u4e8b\u9879<\/h4>\n<p>\/\/ \u26a0\ufe0f \u4e0d\u8981\u4f9d\u8d56 type_info::name() \u7684\u683c\u5f0f<br \/>\n\/\/ \u9519\u8bef\u505a\u6cd5&#xff1a;<br \/>\nif (std::string(typeid(obj).name()) &#061;&#061; &#034;MyClass&#034;) { &#8230; }<\/p>\n<p>\/\/ \u6b63\u786e\u505a\u6cd5&#xff1a;<br \/>\nif (typeid(obj) &#061;&#061; typeid(MyClass)) { &#8230; }<\/p>\n<p>\/\/ \u26a0\ufe0f \u8de8\u52a8\u6001\u5e93\u4f20\u9012 type_info \u5f15\u7528<br \/>\n\/\/ libA.so:<br \/>\nextern const std::type_info&amp; get_ti();<br \/>\n\/\/ libB.so:<br \/>\nif (get_ti() &#061;&#061; typeid(SomeType))  \/\/ \u53ef\u80fd\u5931\u8d25&#xff08;RTLD_LOCAL \u52a0\u8f7d\u65f6&#xff09;<\/p>\n<hr \/>\n<h3 id=\"11-\u5b9e\u6218\u6848\u4f8b\u4e0e\u9677\u9631\">11. \u5b9e\u6218\u6848\u4f8b\u4e0e\u9677\u9631<\/h3>\n<h4 id=\"111-\u9677\u9631\u4e00\u6784\u9020\u51fd\u6570\u4e2d\u7684-dynamic_cast\">11.1 \u9677\u9631\u4e00&#xff1a;\u6784\u9020\u51fd\u6570\u4e2d\u7684 dynamic_cast<\/h4>\n<p>class Base {<br \/>\npublic:<br \/>\n    Base() {<br \/>\n        \/\/ \u5371\u9669&#xff01;\u5728\u6784\u9020\u51fd\u6570\u4e2d\u8c03\u7528 dynamic_cast<br \/>\n        \/\/ \u6b64\u65f6\u865a\u8868\u6307\u5411 Base&#xff0c;\u800c\u975e\u6d3e\u751f\u7c7b<br \/>\n        auto* derived &#061; dynamic_cast&lt;Derived*&gt;(this);<br \/>\n        \/\/ derived \u5c06\u4e3a nullptr<br \/>\n    }<br \/>\n    virtual ~Base() &#061; default;<br \/>\n};<\/p>\n<p>class Derived : public Base {<br \/>\npublic:<br \/>\n    Derived() : Base() {<br \/>\n        \/\/ \u6b64\u65f6\u865a\u8868\u5df2\u7ecf\u6307\u5411 Derived<br \/>\n        auto* d &#061; dynamic_cast&lt;Derived*&gt;(this);<br \/>\n        \/\/ d \u975e\u7a7a<br \/>\n    }<br \/>\n};<\/p>\n<p>\u539f\u7406&#xff1a;C&#043;&#043; \u6807\u51c6\u89c4\u5b9a&#xff0c;\u5728\u6784\u9020\/\u6790\u6784\u671f\u95f4&#xff0c;\u865a\u51fd\u6570\u8868\u6309\u5f53\u524d\u6b63\u5728\u6784\u9020\u7684\u7c7b\u578b\u9010\u6b65\u5207\u6362\u3002\u5728\u00a0Base\u00a0\u6784\u9020\u51fd\u6570\u4e2d&#xff0c;vptr \u6307\u5411\u00a0Base\u00a0\u7684\u865a\u8868&#xff0c;\u56e0\u6b64\u00a0dynamic_cast&lt;Derived*&gt;\u00a0\u8fd4\u56de\u00a0nullptr\u3002<\/p>\n<h4 id=\"112-\u9677\u9631\u4e8c\u7a7a\u6307\u9488\u884c\u4e3a\">11.2 \u9677\u9631\u4e8c&#xff1a;\u7a7a\u6307\u9488\u884c\u4e3a<\/h4>\n<p>Derived* d &#061; nullptr;<br \/>\nBase* b &#061; dynamic_cast&lt;Base*&gt;(d);    \/\/ b \u4e5f\u662f nullptr&#xff0c;\u5408\u6cd5<\/p>\n<p>Base* b_null &#061; nullptr;<br \/>\nDerived* d_null &#061; dynamic_cast&lt;Derived*&gt;(b_null);  \/\/ \u8fd4\u56de nullptr&#xff0c;\u5408\u6cd5<\/p>\n<p>\/\/ \u5bf9\u5f15\u7528\u7c7b\u578b\u7684\u7a7a\u8f6c\u6362\u4f1a\u629b\u51fa\u5f02\u5e38<br \/>\nBase&amp; ref &#061; *b_null;  \/\/ UB&#xff01;\u5148\u89e6\u53d1\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a<\/p>\n<h4 id=\"113-\u9677\u9631\u4e09\u4e0e-static_cast-\u7684\u8bed\u4e49\u5dee\u5f02\">11.3 \u9677\u9631\u4e09&#xff1a;\u4e0e static_cast \u7684\u8bed\u4e49\u5dee\u5f02<\/h4>\n<p>class Base { public: virtual ~Base() &#061; default; };<br \/>\nclass Derived1 : public Base {};<br \/>\nclass Derived2 : public Base {};<\/p>\n<p>Base* b &#061; new Derived1;<\/p>\n<p>\/\/ static_cast&#xff1a;\u65e0\u8fd0\u884c\u65f6\u68c0\u67e5&#xff0c;\u76f4\u63a5\u8f6c\u6362<br \/>\nDerived2* s &#061; static_cast&lt;Derived2*&gt;(b);  \/\/ \u7f16\u8bd1\u901a\u8fc7&#xff01;\u4f46 s \u6307\u5411\u7684\u662f Derived1 \u5bf9\u8c61<br \/>\n\/\/ s-&gt;some_derived2_only_method();  \/\/ UB&#xff01;<\/p>\n<p>\/\/ dynamic_cast&#xff1a;\u8fd0\u884c\u65f6\u68c0\u67e5<br \/>\nDerived2* d &#061; dynamic_cast&lt;Derived2*&gt;(b);  \/\/ \u8fd4\u56de nullptr<br \/>\n\/\/ \u5b89\u5168<\/p>\n<p>\u5f53\u786e\u5b9a\u8f6c\u6362\u5408\u6cd5\u65f6&#xff0c;static_cast\u00a0\u96f6\u5f00\u9500&#xff1b;\u4e0d\u786e\u5b9a\u65f6\u7528\u00a0dynamic_cast\u00a0\u4fdd\u5b89\u5168\u3002<\/p>\n<h4 id=\"114-\u9677\u9631\u56dbreinterpret_cast-\u7ed5\u8fc7\u4fdd\u62a4\">11.4 \u9677\u9631\u56db&#xff1a;reinterpret_cast \u7ed5\u8fc7\u4fdd\u62a4<\/h4>\n<p>Base* b &#061; new Derived1;<br \/>\n\/\/ \u7edd\u5bf9\u4e0d\u8981\u8fd9\u6837\u505a&#xff1a;<br \/>\nDerived2* bad &#061; reinterpret_cast&lt;Derived2*&gt;(b);<br \/>\n\/\/ \u7ed5\u8fc7\u6240\u6709\u7c7b\u578b\u68c0\u67e5&#xff0c;\u5fc5\u5b9a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a<\/p>\n<p>\/\/ \u5373\u4f7f\u6307\u9488\u503c\u53ef\u80fd\u76f8\u540c&#xff0c;\u7c7b\u578b\u7cfb\u7edf\u5df2\u7ecf\u88ab\u7834\u574f<\/p>\n<h4 id=\"115-\u5b9e\u8df5\u5efa\u8bae\u975e\u4fb5\u5165\u5f0f\u7c7b\u578b\u8bc6\u522b\">11.5 \u5b9e\u8df5\u5efa\u8bae&#xff1a;\u975e\u4fb5\u5165\u5f0f\u7c7b\u578b\u8bc6\u522b<\/h4>\n<p>\u5f53 RTTI \u88ab\u7981\u7528\u65f6&#xff0c;\u5e38\u89c1\u66ff\u4ee3\u65b9\u6848&#xff1a;<\/p>\n<p>\/\/ \u65b9\u6848\u4e00&#xff1a;\u624b\u52a8\u7c7b\u578b ID<br \/>\nclass GameObject {<br \/>\npublic:<br \/>\n    enum class Type { Player, Enemy, Projectile, NONE };<br \/>\n    virtual Type getType() const { return Type::NONE; }<br \/>\nprotected:<br \/>\n    Type type_ &#061; Type::NONE;<br \/>\n};<\/p>\n<p>class Player : public GameObject {<br \/>\npublic:<br \/>\n    Player() { type_ &#061; Type::Player; }<br \/>\n    Type getType() const override { return Type::Player; }<br \/>\n};<\/p>\n<p>\/\/ \u4f7f\u7528\u65f6&#xff1a;<br \/>\nGameObject* obj &#061; getObject();<br \/>\nif (obj-&gt;getType() &#061;&#061; GameObject::Type::Player) {<br \/>\n    auto* player &#061; static_cast&lt;Player*&gt;(obj);<br \/>\n    \/\/ &#8230;<br \/>\n}<\/p>\n<p>\/\/ \u65b9\u6848\u4e8c&#xff1a;CRTP &#043; \u7c7b\u578b\u6807\u8bb0&#xff08;\u7f16\u8bd1\u671f\u591a\u6001&#xff09;<br \/>\ntemplate&lt;typename Derived&gt;<br \/>\nclass GameObjectBase {<br \/>\npublic:<br \/>\n    static constexpr int TYPE_ID &#061; -1;<br \/>\n};<\/p>\n<p>class Player : public GameObjectBase&lt;Player&gt; {<br \/>\npublic:<br \/>\n    static constexpr int TYPE_ID &#061; 1;<br \/>\n};<\/p>\n<hr \/>\n<h3 id=\"12-\u603b\u7ed3\">12. \u603b\u7ed3<\/h3>\n<h4 id=\"\u6838\u5fc3\u539f\u7406\u56de\u987e\">\u6838\u5fc3\u539f\u7406\u56de\u987e<\/h4>\n<table>\n<tbody>\n<tr>\n<td style=\"background-color:#dddddd\">\u7ec4\u4ef6<\/td>\n<td style=\"background-color:#dddddd\">\u6838\u5fc3\u673a\u5236<\/td>\n<\/tr>\n<tr>\n<td>type_info<\/td>\n<td>\u7f16\u8bd1\u5668\u4e3a\u6bcf\u4e2a\u591a\u6001\u7c7b\u578b\u5728\u6570\u636e\u6bb5\u751f\u6210\u7684\u5355\u4f8b\u5bf9\u8c61&#xff0c;\u5b58\u50a8\u7c7b\u578b\u540d\u79f0\u548c\u54c8\u5e0c<\/td>\n<\/tr>\n<tr>\n<td>\u865a\u8868 RTTI \u6761\u76ee<\/td>\n<td>vtable[-1] \u5b58\u50a8\u6307\u5411 type_info \u7684\u6307\u9488&#xff0c;\u8fde\u63a5\u8fd0\u884c\u65f6\u5bf9\u8c61\u4e0e\u5176\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>typeid&#xff08;\u591a\u6001&#xff09;<\/td>\n<td>\u901a\u8fc7\u00a0vptr \u2192 vtable[-1] \u2192 type_info*\u00a0\u95f4\u63a5\u83b7\u53d6&#xff0c;O(1)<\/td>\n<\/tr>\n<tr>\n<td>dynamic_cast&#xff08;\u5355\u7ee7\u627f&#xff09;<\/td>\n<td>\u6cbf RTTI \u94fe\u5411\u4e0a\u904d\u5386\u6bd4\u8f83 type_info&#xff0c;O(depth)<\/td>\n<\/tr>\n<tr>\n<td>dynamic_cast&#xff08;\u591a\u7ee7\u627f&#xff09;<\/td>\n<td>\u9012\u5f52\u904d\u5386\u57fa\u7c7b\u6570\u7ec4&#xff0c;\u5229\u7528\u504f\u79fb\u91cf\u4fe1\u606f\u8c03\u6574\u6307\u9488\u503c&#xff0c;O(B)<\/td>\n<\/tr>\n<tr>\n<td>dynamic_cast&#xff08;\u865a\u7ee7\u627f&#xff09;<\/td>\n<td>\u589e\u52a0\u865a\u57fa\u7c7b\u504f\u79fb\u8868\u7684\u8fd0\u884c\u65f6\u67e5\u8be2&#xff0c;\u5f00\u9500\u6700\u5927&#xff0c;O(B) \u00d7 vbase_cost<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"\u8bbe\u8ba1\u53d6\u820d\">\u8bbe\u8ba1\u53d6\u820d<\/h4>\n<p>RTTI \u7684\u8bbe\u8ba1\u4f53\u73b0\u4e86\u4e00\u4e2a\u5178\u578b\u6743\u8861&#xff1a;<\/p>\n<ul>\n<li>\u7a7a\u95f4\u5f00\u9500&#xff1a;\u6bcf\u4e2a\u591a\u6001\u7c7b\u578b\u989d\u5916\u5360\u7528\u7ea6 100-300 \u5b57\u8282\u7684 RTTI \u6570\u636e&#xff08;\u542b type_info \u5bf9\u8c61\u3001\u540d\u5b57\u5b57\u7b26\u4e32\u3001\u57fa\u7c7b\u6570\u7ec4\u7b49&#xff09;<\/li>\n<li>\u65f6\u95f4\u5f00\u9500&#xff1a;typeid\u00a0\u57fa\u672c\u4e3a\u96f6&#xff08;\u6307\u9488\u89e3\u5f15\u7528&#xff09;&#xff0c;dynamic_cast\u00a0\u5e73\u5747 4-25 ns&#xff08;\u53d6\u51b3\u4e8e\u7ee7\u627f\u590d\u6742\u5ea6&#xff09;<\/li>\n<li>\u6536\u76ca&#xff1a;\u63d0\u4f9b\u5b89\u5168\u7684\u8fd0\u884c\u65f6\u7c7b\u578b\u8f6c\u6362&#xff0c;\u662f\u8bb8\u591a\u8bbe\u8ba1\u6a21\u5f0f&#xff08;\u8bbf\u95ee\u8005\u3001\u5de5\u5382\u3001\u5e8f\u5217\u5316\u6846\u67b6&#xff09;\u7684\u57fa\u7840\u8bbe\u65bd<\/li>\n<\/ul>\n<h4 id=\"\u4f7f\u7528\u51c6\u5219\">\u4f7f\u7528\u51c6\u5219<\/h4>\n<li>\u9996\u9009\u8bbe\u8ba1\u907f\u514d dynamic_cast&#xff1a;\u591a\u6001\u865a\u51fd\u6570 &gt; visitor \u6a21\u5f0f &gt;\u00a0dynamic_cast<\/li>\n<li>\u4e0d\u5f97\u4e0d\u4f7f\u7528\u65f6&#xff1a;\u4f18\u5148\u7528\u5f15\u7528\u7248\u672c\u7684\u00a0dynamic_cast&lt;T&amp;&gt;&#xff0c;\u5931\u8d25\u65f6\u629b\u51fa\u00a0std::bad_cast\u00a0\u4fbf\u4e8e\u5b9a\u4f4d\u95ee\u9898<\/li>\n<li>\u6027\u80fd\u654f\u611f\u8def\u5f84&#xff1a;\u4f7f\u7528\u00a0static_cast\u00a0&#043; \u624b\u52a8\u6807\u8bb0\u66ff\u4ee3\u00a0dynamic_cast&#xff08;\u5982\u679a\u4e3e\u7c7b\u578b ID&#xff09;<\/li>\n<li>\u8de8\u6a21\u5757\u8fb9\u754c&#xff1a;\u6ce8\u610f RTTI \u7684\u00a0type_info\u00a0\u53ef\u89c1\u6027\u95ee\u9898&#xff0c;\u5fc5\u8981\u65f6\u4f7f\u7528\u00a0-Wl,-Bsymbolic\u00a0\u6216\u00a0RTLD_GLOBAL<\/li>\n<li>\u5d4c\u5165\u5f0f\u573a\u666f&#xff1a;\u8003\u8651\u00a0-fno-rtti\u00a0\u5e76\u81ea\u5efa\u7c7b\u578b\u6807\u8bc6\u7cfb\u7edf<\/li>\n","protected":false},"excerpt":{"rendered":"<p>1. RTTI \u6982\u5ff5\u4e0e\u542f\u7528\u6761\u4ef6<br \/>\n1.1 \u4ec0\u4e48\u662f RTTI<br \/>\nRTTI&#xff08;Run-Time Type Information \/ Runtime Type Identification&#xff09;\u662f C \u63d0\u4f9b\u7684\u4e00\u5957\u8fd0\u884c\u65f6\u7c7b\u578b\u67e5\u8be2\u673a\u5236&#xff0c;\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u9636\u6bb5\u83b7\u53d6\u5bf9\u8c61\u7684\u5b9e\u9645\u7c7b\u578b\u4fe1\u606f\u3002RTTI \u662f\u00a0typeid\u00a0\u8fd0\u7b97\u7b26\u548c\u00a0dynamic_cast\u00a0\u8fd0\u7b97\u7b26\u7684\u5e95\u5c42\u652f\u6491\u3002<br \/>\n\u4ece\u7f16\u8bd1\u5668\u5b9e\u73b0\u89d2\u5ea6\u770b&#xff0c;RTTI \u5e76\u975e\u4e00\u4e2a\u72ec\u7acb\u7684\u5b50\u7cfb\u7edf&#xff0c;\u800c\u662f\u7531\u4e09<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9099,55,190],"topic":[],"class_list":["post-88007","post","type-post","status-publish","format-standard","hentry","category-server","tag-9099","tag-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ RTTI \u4e0e dynamic_cast \u5e95\u5c42\u5b9e\u73b0\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/88007.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ RTTI \u4e0e dynamic_cast \u5e95\u5c42\u5b9e\u73b0\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"1. RTTI \u6982\u5ff5\u4e0e\u542f\u7528\u6761\u4ef6 1.1 \u4ec0\u4e48\u662f RTTI RTTI&#xff08;Run-Time Type Information \/ Runtime Type Identification&#xff09;\u662f C \u63d0\u4f9b\u7684\u4e00\u5957\u8fd0\u884c\u65f6\u7c7b\u578b\u67e5\u8be2\u673a\u5236&#xff0c;\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u9636\u6bb5\u83b7\u53d6\u5bf9\u8c61\u7684\u5b9e\u9645\u7c7b\u578b\u4fe1\u606f\u3002RTTI \u662f\u00a0typeid\u00a0\u8fd0\u7b97\u7b26\u548c\u00a0dynamic_cast\u00a0\u8fd0\u7b97\u7b26\u7684\u5e95\u5c42\u652f\u6491\u3002 \u4ece\u7f16\u8bd1\u5668\u5b9e\u73b0\u89d2\u5ea6\u770b&#xff0c;RTTI \u5e76\u975e\u4e00\u4e2a\u72ec\u7acb\u7684\u5b50\u7cfb\u7edf&#xff0c;\u800c\u662f\u7531\u4e09\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/88007.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-31T02:32:25+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/88007.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/88007.html\",\"name\":\"C++ RTTI \u4e0e dynamic_cast \u5e95\u5c42\u5b9e\u73b0\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-31T02:32:25+00:00\",\"dateModified\":\"2026-07-31T02:32:25+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/88007.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/88007.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/88007.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ RTTI \u4e0e dynamic_cast \u5e95\u5c42\u5b9e\u73b0\u6df1\u5ea6\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ RTTI \u4e0e dynamic_cast \u5e95\u5c42\u5b9e\u73b0\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/88007.html","og_locale":"zh_CN","og_type":"article","og_title":"C++ RTTI \u4e0e dynamic_cast \u5e95\u5c42\u5b9e\u73b0\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"1. RTTI \u6982\u5ff5\u4e0e\u542f\u7528\u6761\u4ef6 1.1 \u4ec0\u4e48\u662f RTTI RTTI&#xff08;Run-Time Type Information \/ Runtime Type Identification&#xff09;\u662f C \u63d0\u4f9b\u7684\u4e00\u5957\u8fd0\u884c\u65f6\u7c7b\u578b\u67e5\u8be2\u673a\u5236&#xff0c;\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u9636\u6bb5\u83b7\u53d6\u5bf9\u8c61\u7684\u5b9e\u9645\u7c7b\u578b\u4fe1\u606f\u3002RTTI \u662f\u00a0typeid\u00a0\u8fd0\u7b97\u7b26\u548c\u00a0dynamic_cast\u00a0\u8fd0\u7b97\u7b26\u7684\u5e95\u5c42\u652f\u6491\u3002 \u4ece\u7f16\u8bd1\u5668\u5b9e\u73b0\u89d2\u5ea6\u770b&#xff0c;RTTI \u5e76\u975e\u4e00\u4e2a\u72ec\u7acb\u7684\u5b50\u7cfb\u7edf&#xff0c;\u800c\u662f\u7531\u4e09","og_url":"https:\/\/www.wsisp.com\/helps\/88007.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-31T02:32:25+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"17 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/88007.html","url":"https:\/\/www.wsisp.com\/helps\/88007.html","name":"C++ RTTI \u4e0e dynamic_cast \u5e95\u5c42\u5b9e\u73b0\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-31T02:32:25+00:00","dateModified":"2026-07-31T02:32:25+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/88007.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/88007.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/88007.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++ RTTI \u4e0e dynamic_cast \u5e95\u5c42\u5b9e\u73b0\u6df1\u5ea6\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/88007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=88007"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/88007\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=88007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=88007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=88007"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=88007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}