{"id":362032,"date":"2021-02-23T15:29:57","date_gmt":"2021-02-23T07:29:57","guid":{"rendered":"http:\/\/4563.org\/?p=362032"},"modified":"2021-03-06T13:34:42","modified_gmt":"2021-03-06T05:34:42","slug":"c-rtti-%e5%92%8c%e5%9b%9b%e7%a7%8d%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=362032","title":{"rendered":"C++ RTTI \u548c\u56db\u79cd\u7c7b\u578b\u8f6c\u6362"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C++ RTTI \u548c\u56db\u79cd\u7c7b\u578b\u8f6c\u6362               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yiouejv <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>RTTI \u662f Run Time Type Information \u7684\u7f29\u5199\uff0c\u4ece\u5b57\u9762\u4e0a\u6765\u7406\u89e3\u5c31\u662f\u6267\u884c\u65f6\u671f\u7684\u7c7b\u578b\u4fe1\u606f\uff0c\u5176\u91cd\u8981\u4f5c\u7528\u5c31\u662f\u52a8\u6001\u5224\u522b\u6267\u884c\u65f6\u671f\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u5e76\u4e0d\u662f\u8bf4\u8fd9\u7bc7\u6587\u7ae0\u662f RTTI\uff0c\u548c\u7528\u4e8e RTTI \u7684\u56db\u79cd\u7c7b\u578b\u8f6c\u6362\uff0c\u800c\u662f\u4ecb\u7ecd RTTI\uff0c\u518d\u4ecb\u7ecd\u4e00\u4e0b 4 \u79cd\u7c7b\u578b\u8f6c\u6362\uff0c\u56e0\u4e3a RTTI \u6709\u7528\u5230\u5176\u4e2d\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\uff0c\u6240\u4ee5\u76f8\u5f53\u4e8e\u4e24\u7bc7\u6587\u7ae0\u5199\u5728\u4e00\u8d77\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a RTTI \u7528\u5230\u7684\u662f<code>typeid()<\/code> \u548c <code>dynamic_cast()<\/code>\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u4f1a\u6709 RTTI \uff1f<\/strong><\/p>\n<p>C++\u662f\u4e00\u79cd\u9759\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u5176\u6570\u636e\u7c7b\u578b\u662f\u5728\u7f16\u8bd1\u671f\u5c31\u786e\u5b9a\u7684\uff0c\u4e0d\u80fd\u5728\u8fd0\u884c\u65f6\u66f4\u6539\u3002\u7136\u800c\u7531\u4e8e\u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1\u4e2d\u591a\u6001\u6027\u7684\u8981\u6c42\uff0cC++\u4e2d\u7684\u6307\u9488\u6216\u5f15\u7528(Reference)\u672c\u8eab\u7684\u7c7b\u578b\uff0c\u53ef\u80fd\u4e0e\u5b83\u5b9e\u9645\u4ee3\u8868(\u6307\u5411\u6216\u5f15\u7528)\u7684\u7c7b\u578b\u5e76\u4e0d\u4e00\u81f4\u3002\u6709\u65f6\u6211\u4eec\u9700\u8981\u5c06\u4e00\u4e2a\u591a\u6001\u6307\u9488\u8f6c\u6362\u4e3a\u5176\u5b9e\u9645\u6307\u5411\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u5c31\u9700\u8981\u77e5\u9053\u8fd0\u884c\u65f6\u7684\u7c7b\u578b\u4fe1\u606f\uff0c\u8fd9\u5c31\u4ea7\u751f\u4e86\u8fd0\u884c\u65f6\u7c7b\u578b\u8bc6\u522b\u7684\u8981\u6c42\u3002<\/p>\n<p>\u5b9e\u4e8b\u6c42\u662f\u5730\u8bb2\uff0cRTTI \u662f\u6709\u7528\u7684\u3002 \u4f46\u56e0\u4e3a\u4e00\u4e9b\u7406\u8bba\u4e0a\u53ca\u65b9\u6cd5\u8bba\u4e0a\u7684\u539f\u56e0\uff0c\u5b83\u7834\u574f\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u7eaf\u6d01\u6027\u3002<\/p>\n<p>\u9996\u5148\uff0c \u5b83\u7834\u574f\u4e86\u62bd\u8c61\uff0c\u4f7f\u4e00\u4e9b\u672c\u6765\u4e0d\u5e94\u8be5\u88ab\u4f7f\u7528\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u88ab\u4e0d\u6b63\u786e\u5730\u4f7f\u7528\u3002<\/p>\n<p>\u5176\u6b21\uff0c\u56e0\u4e3a\u8fd0\u884c\u65f6\u7c7b\u578b\u7684\u4e0d\u786e\u5b9a\u6027\uff0c\u5b83\u628a\u7a0b\u5e8f\u53d8\u5f97\u66f4\u8106\u5f31\u3002<\/p>\n<p>\u7b2c\u4e09\u70b9\uff0c\u4e5f\u662f\u6700\u91cd\u8981\u7684\u4e00\u70b9\uff0c\u5b83\u4f7f\u7a0b\u5e8f\u7f3a\u4e4f\u6269\u5c55\u6027\u3002 \u5f53\u52a0\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u7c7b\u578b\u65f6\uff0c\u4f60\u4e5f\u8bb8\u9700\u8981\u4ed4\u7ec6\u9605\u8bfb\u4f60\u7684<code>dynamic_cast<\/code>\u7684\u4ee3\u7801\uff0c\u5fc5\u8981\u65f6\u6539\u52a8\u5b83\u4eec\uff0c\u4ee5\u4fdd\u8bc1\u8fd9\u4e2a\u65b0\u7684\u7c7b\u578b\u7684\u52a0\u5165\u4e0d\u4f1a\u5bfc\u81f4\u95ee\u9898\u3002 \u800c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u7f16\u8bd1\u5668\u5c06\u4e0d\u4f1a\u7ed9\u4f60\u4efb\u4f55\u5e2e\u52a9\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4\uff0cRTTI \u56e0\u4e3a\u5b83\u7684\u65b9\u6cd5\u8bba\u4e0a\u7684\u4e00\u4e9b\u7f3a\u70b9\uff0c\u5b83\u5fc5\u987b\u88ab\u975e\u5e38\u8c28\u614e\u5730\u4f7f\u7528\u3002 \u4eca\u5929\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u7684\u7c7b\u578b\u7cfb\u7edf\u4e2d\u7684\u5f88\u591a\u4e1c\u897f\u5c31\u662f\u4ea7\u751f\u4e8e\u907f\u514d RTTI \u7684\u5404\u79cd\u52aa\u529b\u3002<\/p>\n<p>\u9996\u5148\u6211\u4eec\u6765\u4e2a\u4f8b\u5b50\u611f\u53d7\u4e00\u4e0b:<\/p>\n<pre><code>#include&lt;iostream&gt; #include&lt;typeinfo&gt; using namespace std;  class Base { public:     virtual void funcA() { cout &lt;&lt; \"Base\" &lt;&lt; endl; } };  class Derived : public Base { public:     virtual void funcB() { cout &lt;&lt; \"Derived\" &lt;&lt; endl; } };  void funcC(Base* p) {     Derived* dp = dynamic_cast&lt;Derived*&gt;(p);     if (dp != NULL) {         dp-&gt;funcB();     }     else {         p-&gt;funcA();     } };  void funcD(Base* p) {     Derived* dp = NULL;     if (typeid(*p) == typeid(Derived))     {         dp = static_cast&lt;Derived*&gt;(p);         dp-&gt;funcB();     }     else {         p-&gt;funcA();     } }  int main(int argc, char const* argv[]) {     Base* p = new Derived;     cout &lt;&lt; typeid(p).name() &lt;&lt; endl;     cout &lt;&lt; typeid(*p).name() &lt;&lt; endl;     funcD(p);     funcC(p);     delete p;      Base* dp = new Base;     funcC(dp);     funcD(dp);     delete dp;     return 0; } <\/code><\/pre>\n<p>funcC \u662f\u7528<code>dynamic_cast<\/code>\u7c7b\u578b\u8f6c\u6362\u662f\u5426\u6210\u529f\u6765\u8bc6\u522b\u7c7b\u578b\u7684\uff0c<code>dynamic_cast<\/code>\u64cd\u4f5c\u7b26\u5c06\u57fa\u7c7b\u7c7b\u578b\u5bf9\u8c61\u7684\u5f15\u7528\u6216\u6307\u9488\u8f6c\u5316\u4e3a\u540c\u4e00\u7ee7\u627f\u5c42\u6b21\u4e2d\u7684\u5176\u4ed6\u7c7b\u578b\u7684\u5f15\u7528\u6216\u6307\u9488\u3002 <\/p>\n<p>\u5982\u679c\u7ed1\u5b9a\u5230\u5f15\u7528\u6216\u6307\u9488\u7684\u5bf9\u8c61\u4e0d\u662f\u76ee\u6807\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5219<code>dynamic_cast<\/code>\u5931\u8d25\u3002<br \/> \u5982\u679c\u8f6c\u6362\u5230\u6307\u9488\u7c7b\u578b\u7684<code>dynamic_cast<\/code>\u5931\u8d25\uff0c\u5219<code>dynamic_cast<\/code>\u7684\u7ed3\u679c\u662f<code>NULL<\/code>\u503c;<br \/> \u5982\u679c\u8f6c\u6362\u5230\u5f15\u7528\u7c7b\u578b\u7684<code>dynamic_cast<\/code>\u5931\u8d25\uff0c \u5219\u629b\u51fa\u4e00\u4e2a<code>bad_cast<\/code>\u7c7b\u578b\u7684\u5f02\u5e38 <\/p>\n<p>funcD \u662f\u7528<code>typeid<\/code>\u5224\u65ad\u57fa\u7c7b\u5730\u5740\u662f\u5426\u4e00\u81f4\u7684\u529e\u6cd5\u6765\u8bc6\u522b\u7c7b\u578b\u7684\u3002<\/p>\n<h3>typeid<\/h3>\n<p>\u4e0b\u9762\u6211\u4eec\u5177\u4f53\u8bf4\u8bf4 <code>typeid<\/code><\/p>\n<p><code>typeid<\/code>\u662f C++\u7684\u5173\u952e\u5b57\u4e4b\u4e00, \u7b49\u540c\u4e8e<code>sizeof<\/code>\u8fd9\u7c7b operator \u3002typeid \u64cd\u4f5c\u7b26\u7684\u8fd4\u56de\u7ed3\u679c\u662f\u540d\u4e3a <code>type_info<\/code>\u7684\u6807\u51c6\u5e93\u7c7b\u578b\u7684\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u5728\u5934\u6587\u4ef6 typeinfo \u4e2d\u5b9a\u4e49\u3002 \u6709\u4e24\u79cd\u5f62\u5f0f:<\/p>\n<ol>\n<li>typeid(type)<\/li>\n<li>typeid(expression)<\/li>\n<\/ol>\n<p>\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u662f\u7c7b\u7c7b\u578b\uff0c\u4e14\u81f3\u5c11\u542b\u6709\u4e00\u4e2a\u865a\u51fd\u6570\uff0c\u5219 typeid \u64cd\u4f5c\u7b26\u8fd4\u56de\u8868\u8fbe\u5f0f\u7684\u52a8\u6001\u7c7b\u578b\uff0c\u9700\u8981\u5728\u8fd0\u884c\u65f6\u8ba1\u7b97\uff0c\u5426\u5219\u8fd4\u56de\u8868\u8fbe\u5f0f\u7684\u9759\u6001\u7c7b\u578b\uff0c\u5728\u7f16\u8bd1\u65f6\u5c31\u53ef\u4ee5\u8ba1\u7b97\u3002<\/p>\n<p>C++\u6807\u51c6\u89c4\u5b9a\u4e86\u5176\u5b9e\u73b0\u5fc5\u987b\u63d0\u4f9b\u5982\u4e0b\u56db\u79cd\u64cd\u4f5c:<\/p>\n<ol>\n<li>t1 == t2: \u5982\u679c\u4e24\u4e2a\u5bf9\u8c61 t1 \u548c t2 \u7c7b\u578b\u76f8\u540c\uff0c\u5219\u8fd4\u56de true;\u5426\u5219\u8fd4\u56de false<\/li>\n<li>t1 != t2: \u5982\u679c\u4e24\u4e2a\u5bf9\u8c61 t1 \u548c t2 \u7c7b\u578b\u4e0d\u540c\uff0c\u5219\u8fd4\u56de true;\u5426\u5219\u8fd4\u56de false<\/li>\n<li>t.name(): \u8fd4\u56de\u7c7b\u578b\u7684 c-style \u5b57\u7b26\u4e32\uff0c\u7c7b\u578b\u540d\u5b57\u7528\u7cfb\u7edf\u76f8\u5173\u7684\u65b9\u6cd5\u4ea7\u751f<\/li>\n<li>t1.before(t2): \u8fd4\u56de\u6307\u51fa t1 \u662f\u5426\u51fa\u73b0\u5728 t2 \u4e4b\u524d \u7684 bool \u503c<\/li>\n<\/ol>\n<p><code>type_info<\/code>\u7c7b\u63d0\u4f9b\u4e86 public \u865a\u6790\u6784\u51fd\u6570\uff0c\u4ee5\u4f7f\u7528\u6237\u80fd\u591f\u7528\u5176\u4f5c\u4e3a\u57fa\u7c7b\u3002\u5b83\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u548c\u590d\u5236\u6784\u9020\u51fd\u6570\u53ca\u8d4b\u503c\u64cd\u4f5c\u7b26\u90fd\u5b9a\u4e49\u4e3a private\uff0c\u6240\u4ee5\u4e0d\u80fd\u5b9a\u4e49\u6216\u590d\u5236 type_info \u7c7b\u578b\u7684\u5bf9\u8c61\u3002 \u7a0b\u5e8f\u4e2d\u521b\u5efa type_info \u5bf9\u8c61\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u4f7f\u7528 typeid \u64cd\u4f5c\u7b26\u3002 \u7531\u6b64\u53ef\u89c1\uff0c\u5982\u679c\u628a typeid \u770b\u4f5c\u51fd\u6570\u7684\u8bdd\uff0c\u5176\u5e94\u8be5\u662f type_info \u7684\u53cb\u5143\u3002 \u8fd9\u5177\u4f53\u7531\u7f16\u8bd1\u5668\u7684\u5b9e\u73b0\u6240\u51b3\u5b9a\uff0c\u6807\u51c6\u53ea\u8981\u6c42\u5b9e\u73b0\u4e3a\u6bcf\u4e2a\u7c7b\u578b\u8fd4\u56de\u552f\u4e00\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u4f8b 1: C++\u91cc\u9762\u7684 typeid \u8fd0\u7b95\u7b26\u8fd4\u56de\u503c\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u7b54: \u5e38\u91cf\u5bf9\u8c61\u7684\u5f15\u7528\u3002<\/p>\n<p>\u5982\u679c p \u662f\u57fa\u7c7b\u6307\u9488\uff0c\u5e76\u4e14\u6307\u5411\u4e00\u4e2a\u6d3e\u751f\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5e76\u4e14\u57fa\u7c7b\u4e2d\u6709\u865a\u51fd\u6570\uff0c\u90a3\u4e48<code>typeid(*p)<\/code>\u8fd4\u56de p \u6240\u6307\u5411\u7684\u6d3e\u751f\u7c7b\u7c7b\u578b\uff0c<code>typeid(p)<\/code>\u8fd4\u56de\u57fa\u7c7b\u7c7b\u578b\u3002<\/p>\n<p><strong>RTTI \u7684\u5b9e\u73b0\u539f\u7406<\/strong>: \u901a\u8fc7\u5728\u865a\u8868\u4e2d\u653e\u4e00\u4e2a\u989d\u5916\u7684\u6307\u9488\uff0c\u6bcf\u4e2a\u65b0\u7c7b\u53ea\u4ea7\u751f\u4e00\u4e2a typeinfo \u5b9e\u4f8b\uff0c\u989d\u5916\u6307\u9488\u6307\u5411 typeinfo, typeid \u8fd4\u56de\u5bf9\u5b83\u7684\u4e00\u4e2a\u5f15\u7528\u3002<\/p>\n<h3>static_cast<\/h3>\n<p><code>static_cast &lt; new_type &gt; ( expression ) <\/code><\/p>\n<p>\u672c\u6765\u5e94\u8be5\u5148\u8ba8\u8bba<code>dynamic_cast<\/code>\u7684\uff0c\u56e0\u4e3a\u54b1\u4eec\u672c\u6765\u804a\u7684 RTTI \u561b\uff0c\u4f46\u662f\u5148\u4e86\u89e3\u4e00\u4e0b<code>static_cast<\/code>\uff0c\u7136\u540e\u770b\u548c<code>dynamic_cast<\/code>\u7684\u6bd4\u8f83\u53ef\u80fd\u66f4\u597d\u4e00\u70b9\u3002<\/p>\n<p>\u4f7f\u7528\u573a\u666f:<\/p>\n<ol>\n<li>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362<\/li>\n<li>initializing conversion<\/li>\n<\/ol>\n<pre><code>int n = static_cast&lt;int&gt;(3.14); cout &lt;&lt; \"n = \" &lt;&lt; n &lt;&lt; 'n'; vector&lt;int&gt; v = static_cast&lt;vector&lt;int&gt;&gt;(10); cout &lt;&lt; \"v.size() = \" &lt;&lt; v.size() &lt;&lt; 'n'; <\/code><\/pre>\n<ol>\n<li>\u7c7b\u6307\u9488\u6216\u5f15\u7528\u5411\u4e0b\u8f6c\u6362\u3002<\/li>\n<li>\u5c06\u7c7b\u578b\u8f6c\u4e3a\u53f3\u503c\u7c7b\u578b\uff0c\u8fdb\u884c move \u64cd\u4f5c\uff0c\u8fd9\u4e2a\u5728\u6807\u51c6\u5e93\u4e2d\u6709\u4f53\u73b0\uff08\u653e\u5728\u4e0b\u4e00\u7bc7\u6587\u7ae0\u4e2d\u89e3\u91ca\uff09<\/li>\n<\/ol>\n<pre><code>vector&lt;int&gt; v = static_cast&lt;vector&lt;int&gt;&gt;(10); vector&lt;int&gt; v2 = static_cast&lt;vector&lt;int&gt;&amp;&amp;&gt;(v); cout &lt;&lt; \"after move, v.size() = \" &lt;&lt; v.size() &lt;&lt; 'n'; cout &lt;&lt; v.size() &lt;&lt; endl; <\/code><\/pre>\n<ol>\n<li>\u5b50\u7c7b\u6570\u7ec4\u6307\u9488\u5411\u4e0a\u8f6c\u6210\u7236\u7c7b\u7684\u6307\u9488<\/li>\n<\/ol>\n<pre><code>struct B {     int m = 0;     void hello() const {         cout &lt;&lt; \"Hello world, this is B!n\";     } }; struct D : B {     void hello() const {         cout &lt;&lt; \"Hello world, this is D!n\";     } };  D a[10]; B* dp = static_cast&lt;B*&gt;(a); dp-&gt;hello(); <\/code><\/pre>\n<ol>\n<li>\u679a\u4e3e\u8f6c\u6362\u6210 int or float<\/li>\n<\/ol>\n<pre><code>enum E { ONE = 1, TWO, THREE }; E e = E::ONE; int one = static_cast&lt;int&gt;(e); cout &lt;&lt; one &lt;&lt; 'n'; <\/code><\/pre>\n<ol>\n<li>int to enum, enum to another emum<\/li>\n<\/ol>\n<pre><code>enum class E { ONE = 1, TWO, THREE }; enum EU { ONE = 1, TWO, THREE };  E e = E::ONE; int one = static_cast&lt;int&gt;(e); E e2 = static_cast&lt;E&gt;(one); EU eu = static_cast&lt;EU&gt;(e2); <\/code><\/pre>\n<ol>\n<li>void* to any type<\/li>\n<\/ol>\n<pre><code>int a = 100; void* voidp = &amp;a; int *p = static_cast&lt;int*&gt;(voidp); <\/code><\/pre>\n<p>\u6ce8\u610f\uff1a<\/p>\n<ol>\n<li>static_cast \u4e0d\u80fd\u8f6c\u6362\u6389 expression \u7684 const \u3001volatile \u548c__unaligned \u5c5e\u6027,<\/li>\n<li>\u7f16\u8bd1\u5668\u9690\u5f0f\u6267\u884c\u4efb\u4f55\u7c7b\u578b\u8f6c\u6362\u90fd\u53ef\u7531 static_cast \u663e\u793a\u5b8c\u6210<\/li>\n<\/ol>\n<h3>dynamic_cast<\/h3>\n<p><code>dynamic_cast &lt; new-type &gt; ( expression )<\/code><\/p>\n<p>\u63a5\u4e0b\u6765\u662f<code>dynamic_cast<\/code>:<\/p>\n<p>\u52a8\u6001\u6620\u5c04\u53ef\u4ee5\u6620\u5c04\u5230\u4e2d\u95f4\u5c42\u7ea7\uff0c\u5c06\u6d3e\u751f\u7c7b\u6620\u5c04\u5230\u4efb\u4f55\u4e00\u4e2a\u57fa\u7c7b\uff0c\u7136\u540e\u5728\u57fa\u7c7b\u4e4b\u95f4\u53ef\u4ee5\u76f8\u4e92\u6620\u5c04\u3002<\/p>\n<p><strong>dynamic_cast \u5b9e\u73b0\u539f\u7406<\/strong>: \u5148\u6062\u590d\u6e90\u6307\u9488\u7684 RTTI \u4fe1\u606f\uff0c\u518d\u53d6\u76ee\u6807\u7684 RTTT \u4fe1\u606f\uff0c\u6bd4\u8f83\u4e24\u8005\u662f\u5426\u76f8\u540c\uff0c\u4e0d\u540c\u53d6\u76ee\u6807\u7c7b\u578b\u7684\u57fa\u7c7b; \u7531\u4e8e\u5b83\u9700\u8981\u68c0\u67e5\u4e00\u957f\u4e32\u57fa\u7c7b\u5217\u8868\uff0c\u6545\u52a8\u6001\u6620\u5c04\u7684\u5f00\u9500\u6bd4 typeid \u5927\u3002 <\/p>\n<p><strong>dynamic_cast \u7684\u5b89\u5168\u6027<\/strong>: \u5982\u5b9e\u73b0\u539f\u7406\u6240\u8bf4\uff0c<code>dynamic_cast<\/code>\u4f1a\u505a\u4e00\u7cfb\u5217\u7684\u7c7b\u578b\u68c0\u67e5\uff0c\u8f6c\u6362\u6210\u529f\u4f1a\u8fd4\u56de\u76ee\u6807\u7c7b\u578b\u6307\u9488\uff0c\u5931\u8d25\u5219\u4f1a\u8fd4\u56de NULL, \u76f8\u5bf9\u4e8e<code>static_cast<\/code>\u5b89\u5168\uff0c\u56e0\u4e3a <code>static_cast<\/code>\u5373\u4f7f\u8f6c\u6362\u5931\u8d25\u4e5f\u4e0d\u4f1a\u8fd4\u56de NULL \u3002<\/p>\n<p><strong>\u4f8b 2: \u8fd9\u79cd\u60c5\u51b5\u4e0b static_cast() \u4e5f\u662f\u5b89\u5168\u7684\u3002<\/strong><\/p>\n<pre><code>class Base { public:     void func() { cout &lt;&lt; \"Base func\" &lt;&lt; endl; } };  class Derived : public Base { public:     void func() { cout &lt;&lt; \"Derived func\" &lt;&lt; endl; } };  int main(int argc, char const* argv[]) {     Derived *pd = new Derived;     pd-&gt;func();     Base* pb1 = dynamic_cast&lt;Base*&gt;(pd);     pb1-&gt;func();     Base* pb2 = static_cast&lt;Base*&gt;(pd);     pb2-&gt;func();     return 0; } <\/code><\/pre>\n<p>pd \u6307\u9488\u6307\u5411\u7684\u5185\u5b58\u662f\u5b50\u7c7b\u5bf9\u8c61\uff0c\u6211\u4eec\u77e5\u9053\uff0c\u7ee7\u627f\u5b50\u7c7b\u662f\u5305\u542b\u7236\u7c7b\u7684\uff0c\u76f8\u5f53\u4e8e\u5728\u7236\u7c7b\u7684\u57fa\u7840\u4e0a\u5728\u6dfb\u52a0\u5b50\u7c7b\u7684\u6210\u5458\uff08\u5982\u679c\u4f60\u8fd8\u4e0d\u6e05\u695a\u7684\u8bdd\uff0c\u5efa\u8bae\u4f60\u770b\u4e00\u4e0b\u6211\u4e4b\u524d\u7684\u6587\u7ae0: \u865a\u51fd\u6570\uff0c\u865a\u8868\u6df1\u5ea6\u5256\u6790\uff09\u3002 \u6240\u4ee5 pd \u6307\u9488\u8f6c\u6210\u7236\u7c7b\u6307\u9488\u4e5f\u662f\u6ca1\u95ee\u9898\u7684\uff0c<code>static_cast<\/code>\u4e5f\u4e00\u6837\u5b89\u5168\u3002<\/p>\n<p>\u76f8\u53cd\uff0c\u5982\u679c\u6307\u9488\u6307\u5411\u7684\u5185\u5b58\u662f\u7236\u7c7b\u6210\u5458\uff0c\u8f6c\u6210\u5b50\u7c7b\u6307\u9488\uff0c<code>dynamic_cast<\/code> \u5219\u4f1a\u5931\u8d25\uff0c\u8fd4\u56de NULL, \u4f46\u662f<code>static_cast<\/code>\u4e0d\u4f1a\u5931\u8d25\uff0c\u5f3a\u5236\u8f6c\u8fc7\u53bb\u4e86\uff0c\u5982\u679c\u6b64\u65f6\u5b50\u7c7b\u6307\u9488\u8bbf\u95ee\u7236\u7c7b\u4e2d\u4e0d\u5b58\u5728\uff0c\u4f46\u662f\u5b50\u7c7b\u4e2d\u5b58\u5728\u7684\u6210\u5458\uff0c\u5219\u4f1a\u53d1\u751f\u610f\u60f3\u4e0d\u5230\u7684\u95ee\u9898\u3002<\/p>\n<p>\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50:<\/p>\n<pre><code>class Base { public:     virtual void func() { cout &lt;&lt; \"Base func\" &lt;&lt; endl; } };  class Derived : public Base { public:     virtual void func() { cout &lt;&lt; \"Derived func\" &lt;&lt; endl; }     int m_value = 0; };  int main(int argc, char const* argv[]) {     Base *pb = new Base;     pb-&gt;func();     Derived* pd1 = dynamic_cast&lt;Derived*&gt;(pb);     if (pd1 != NULL) {         pd1-&gt;func();     } else {         cout &lt;&lt; \"dynamic_cast failed\" &lt;&lt; endl;     }     Derived* pd2 = static_cast&lt;Derived*&gt;(pb);     pd2-&gt;func();     cout &lt;&lt; \"m_value: \" &lt;&lt; pd2-&gt;m_value &lt;&lt; endl;     return 0; } <\/code><\/pre>\n<p>\u8f93\u51fa:<\/p>\n<pre><code>Base func dynamic_cast failed Base func  \/\/ \u7236\u7c7b\u4e2d\u4e5f\u6709\u8fd9\u4e2a\u865a\u51fd\u6570\uff0c\u6240\u4ee5 static_cast \u8f6c\u6362\u8c03\u7528\u6ca1\u51fa\u95ee\u9898\u3002 m_value: -33686019  \/\/ \u8fd9\u91cc\u51fa\u95ee\u9898\u4e86 <\/code><\/pre>\n<p>\u5bf9\u4e8e\u4e0a\u884c\u8f6c\u6362\uff0c<code>static_cast<\/code> \u548c <code>dynamic_cast<\/code> \u6548\u679c\u4e00\u6837\uff0c\u90fd\u5b89\u5168, \u5982\u679c\u53ea\u662f\u5355\u7eaf\u7684\u5411\u4e0a\u8f6c\u7684\u8bdd\uff0c\u6ca1\u5fc5\u8981\uff0c\u76f4\u63a5\u7528\u865a\u51fd\u6570\u5b9e\u73b0\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u5bf9\u4e8e\u4e0b\u884c\u8f6c\u6362: \u4f60\u5fc5\u987b\u786e\u5b9a\u8981\u8f6c\u6362\u7684\u6570\u636e\u786e\u5b9e\u662f\u76ee\u6807\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5373\u9700\u8981\u6ce8\u610f\u8981\u8f6c\u6362\u7684\u7236\u7c7b\u7c7b\u578b\u6307\u9488\u662f\u5426\u771f\u7684\u6307\u5411\u5b50\u7c7b\u5bf9\u8c61\uff0c\u5982\u679c\u662f\uff0cstatic_cast \u548c dynamic_cast \u90fd\u80fd\u6210\u529f\uff1b\u5982\u679c\u4e0d\u662f static_cast \u80fd\u8fd4\u56de\uff0c\u4f46\u662f\u4e0d\u5b89\u5168\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u8bbf\u95ee\u8d8a\u754c\u9519\u8bef\uff0c\u800c dynamic_cast \u5728\u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5\u8fc7\u7a0b\u4e2d\uff0c\u5224\u5b9a\u8be5\u8fc7\u7a0b\u4e0d\u80fd\u8f6c\u6362\uff0c\u8fd4\u56de NULL \u3002<\/p>\n<h3>const_cast<\/h3>\n<p><code>const_cast &lt; new_type &gt; ( expression )<\/code><\/p>\n<p>\u4e0a\u9762\u8bb2\u4e86<code>static_cast<\/code>\u662f\u4e0d\u80fd\u53bb\u6389 <code>const<\/code>\uff0c\u800c <code>const_cast<\/code>\u662f\u4e13\u95e8\u7528\u6765\u53bb\u6389 <code>const<\/code>\u3002<\/p>\n<p>\u800c\u6dfb\u52a0 const, <code>static_cast<\/code> \u4e5f\u662f\u53ef\u4ee5\u6dfb\u52a0\u4e0a const\uff0c\u53ea\u662f\u4e0d\u80fd\u53bb\u6389 const<\/p>\n<p>\u770b\u4e0b\u9762\u4e00\u4e2a\u4f8b\u5b50:<\/p>\n<pre><code>const int a = 26; const int* pa = &amp;a; \/\/*pa = 3;  \/\/ \u7f16\u8bd1\u4e0d\u8fc7\uff0c\u6307\u9488\u5e38\u91cf\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u4fee\u6539\u503c  int* b = const_cast&lt;int*&gt;(pa);  \/\/ \u628a const \u8f6c\u6362\u6389 *b = 3;  cout &lt;&lt; \"a: \" &lt;&lt; a &lt;&lt; endl;  \/\/ 26 cout &lt;&lt; \"*b: \" &lt;&lt; *b &lt;&lt; endl;  \/\/ 3 <\/code><\/pre>\n<p>a \u4e3a <code>const int<\/code> \u7c7b\u578b\uff0c\u4e0d\u53ef\u4fee\u6539\uff0cpa \u4e3a <code>const int*<\/code> \u7c7b\u578b\uff0c\u4e0d\u80fd\u901a\u8fc7 pa \u6307\u9488\u4fee\u6539 a \u7684\u503c<\/p>\n<p>b \u901a\u8fc7 <code>const_cast<\/code> \u8f6c\u6362\u6389\u4e86 const\uff0c\u6210\u529f\u4fee\u6539\u4e86 a \u7684\u503c\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/03\/20210302154626513.pngpic_center.png\" alt=\"C++ RTTI \u548c\u56db\u79cd\u7c7b\u578b\u8f6c\u6362\" \/><\/p>\n<p>\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u4e3a\u4ec0\u4e48 a \u8f93\u51fa\u662f 26 \u5462\uff1f<\/p>\n<p>\u5982\u679c\u5b58\u5728<code>const int x = 26;<\/code> \u8fd9\u79cd\u60c5\u51b5\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u4f1a\u8ba4\u4e3a x \u662f\u4e00\u4e2a\u7f16\u8bd1\u671f\u53ef\u8ba1\u7b97\u51fa\u7ed3\u679c\u7684\u5e38\u91cf\uff0c\u90a3\u4e48 x \u5c31\u4f1a\u50cf\u5b8f\u5b9a\u4e49\u4e00\u6837\uff0c\u7528\u5230 x \u7684\u5730\u65b9\u4f1a\u88ab\u7f16\u8bd1\u5668\u66ff\u6362\u6210 26 \u3002<\/p>\n<p>\u4e0a\u8ff0\u8fd9\u4e2a\u4f8b\u5b50\u4e0d\u5efa\u8bae\u4f7f\u7528\uff0c\u56e0\u4e3a a \u58f0\u660e\u4e3a <code>const int<\/code> \u7c7b\u578b\uff0c\u5b9e\u9645\u4e0a\u662f\u5e76\u4e0d\u5e0c\u671b\u88ab\u4fee\u6539\u7684\uff0c\u8fd9\u6837\u5f3a\u884c\u4fee\u6539\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9879\u76ee\u91cc\u4e0d\u53ef\u9884\u671f\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>const_cast \u7684\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n<ol>\n<li>\u5982\u679c\u6709\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u7684\u5f62\u53c2\u662f non-const \u7c7b\u578b\u53d8\u91cf\uff0c\u800c\u4e14\u51fd\u6570\u4e0d\u4f1a\u5bf9\u5b9e\u53c2\u7684\u503c\u8fdb\u884c\u6539\u52a8\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u4e3a const \u7684\u53d8\u91cf\u6765\u8c03\u7528\u51fd\u6570\u3002<\/li>\n<\/ol>\n<pre><code>void func(int* a) { }  int main() {     const int a = 26;     const int* pa = &amp;a;     func(const_cast&lt;int*&gt;(pa)); } <\/code><\/pre>\n<p>\u8fd9\u79cd\u60c5\u51b5\u5176\u5b9e\u6211\u89c9\u5f97\u6ca1\u5fc5\u8981\uff0c\u5b9e\u9645\u4e0a\u6211\u4e0d\u60f3\u6539\u7684\u8bdd\uff0c\u6211\u5f62\u53c2\u52a0 const\uff0c\u628a\u524d\u63d0\u63a8\u7ffb\u4e0d\u5c31\u884c\u4e86\uff0c\u8fd8\u5b89\u5168\u3002<\/p>\n<ol>\n<li>\u5b9a\u4e49\u4e86\u4e00\u4e2a non-const \u7684\u53d8\u91cf\uff0c\u5374\u4f7f\u7528\u4e86\u4e00\u4e2a\u6307\u5411 const \u503c\u7684\u6307\u9488\u6765\u6307\u5411\u5b83\uff08\u8fd9\u4e0d\u662f\u6ca1\u4e8b\u627e\u4e8b\u561b\uff09\uff0c\u5728\u7a0b\u5e8f\u7684\u67d0\u5904\u6211\u4eec\u60f3\u6539\u53d8\u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\u4e86\uff0c\u4f46\u624b\u5934\u53ea\u6301\u6709\u6307\u9488\uff0c\u8fd9\u65f6<code>const_cast<\/code>\u5c31\u53ef\u4ee5\u7528\u5230\u4e86<\/li>\n<\/ol>\n<pre><code>int a = 26; const int* pa = &amp;a; \/\/ *pa = 1; \u7f16\u8bd1\u4e0d\u8fc7 int* pa2 = const_cast&lt;int*&gt;(pa); *pa2 = 1; <\/code><\/pre>\n<h3>reinterpret_cast<\/h3>\n<p><code>reinterpret_cast &lt; new_type &gt; ( expression )<\/code><\/p>\n<p>reinterpret_ cast \u901a\u5e38\u4e3a\u64cd\u4f5c\u6570\u7684\u4f4d\u6a21\u5f0f\u63d0\u4f9b\u8f83\u4f4e\u5c42\u7684\u91cd\u65b0\u89e3\u91ca\u3002<\/p>\n<p>\u770b\u8fd9\u4e2a\u4f8b\u5b50:<\/p>\n<pre><code>int n = 9; double d = static_cast&lt;double&gt;(n);  cout &lt;&lt; n &lt;&lt; \" \" &lt;&lt; d; \/\/ \u8f93\u51fa 9 9  int n2 = 9; double d2 = reinterpret_cast&lt;double&amp;&gt;(n2); cout &lt;&lt; n2 &lt;&lt; \" \" &lt;&lt; d2; \/\/ \u8f93\u51fa 9 -9.25596e+61 <\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a\u53d8\u5740\u4ece int \u8f6c\u6362\u5230 double \u3002\u8fd9\u4e9b\u7c7b\u578b\u7684\u4e8c\u8fdb\u5236\u8868\u8fbe\u5f0f\u662f\u4e0d\u540c\u7684\u3002\u8981\u5c06\u6574\u6570 9 \u8f6c\u6362\u5230\u53cc\u7cbe\u5ea6\u6574\u6570 9, static_cast \u9700\u8981\u6b63\u786e\u5730\u4e3a\u53cc\u7cbe\u5ea6\u6574\u6570 d \u8865\u8db3\u6bd4\u7279\u4f4d\u3002\u5176\u7ed3\u679c\u4e3a 9.0 \u3002<\/p>\n<p>\u800c reinterpret_cast \u7684\u884c\u4e3a\u5374\u4e0d\u540c\uff0c\u4ec5\u4ec5\u662f\u628a\u5185\u5b58\u62f7\u8d1d\u5230\u76ee\u6807\u7a7a\u95f4\uff0c\u89e3\u91ca\u51fa\u6765\u662f\u4e00\u4e2a\u5927\u6570\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/03\/20210306_6043144820bde.png\" alt=\"C++ RTTI \u548c\u56db\u79cd\u7c7b\u578b\u8f6c\u6362\" \/> <img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/03\/20210306_6043144d7248e.png\" alt=\"C++ RTTI \u548c\u56db\u79cd\u7c7b\u578b\u8f6c\u6362\" \/> <img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/03\/20210306_604314542ea85.png\" alt=\"C++ RTTI \u548c\u56db\u79cd\u7c7b\u578b\u8f6c\u6362\" \/><\/p>\n<p>reinterpret_cast \u8fd9\u4e2a\u64cd\u4f5c\u7b26\u88ab\u7528\u4e8e\u7684\u7c7b\u578b\u8f6c\u6362\u7684\u8f6c\u6362\u7ed3\u679c\u51e0\u4e4e\u90fd\u662f\u672a\u77e5\u7684\u3002<\/p>\n<p>\u4f7f\u7528 reinterpret_cast \u7684\u4ee3\u7801\u5f88\u96be\u79fb\u690d\u3002\u8f6c\u6362\u51fd\u6570\u6307\u9488\u7684\u4ee3\u7801\u662f\u4e0d\u53ef\u79fb\u690d\u7684\uff0c\uff08 C++\u4e0d\u4fdd\u8bc1\u6240\u6709\u7684\u51fd\u6570\u6307\u9488\u90fd\u88ab\u7528\u4e00\u6837\u7684\u65b9\u6cd5\u8868\u793a\uff09\uff0c\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u8fd9\u6837\u7684\u8f6c\u6362\u4f1a\u4ea7\u751f\u4e0d\u6b63\u786e\u7684\u7ed3\u679c\u3002 \u6240\u4ee5\u5e94\u8be5\u907f\u514d\u8f6c\u6362\u51fd\u6570\u6307\u9488\u7c7b\u578b\uff0c\u6309\u7167 C++\u65b0\u601d\u7ef4\u7684\u8bdd\u6765\u8bf4\uff0creinterpret_cast \u662f\u4e3a\u4e86\u6620\u5c04\u5230\u4e00\u4e2a\u5b8c\u5168\u4e0d\u540c\u7c7b\u578b\u7684\u610f\u601d\uff0c\u8fd9\u4e2a\u5173\u952e\u8bcd\u5728\u6211\u4eec\u9700\u8981\u628a\u7c7b\u578b\u6620\u5c04\u56de\u539f\u6709\u7c7b\u578b\u65f6\u7528\u5230\u5b83\u3002 \u6211\u4eec\u6620\u5c04\u5230\u7684\u7c7b\u578b\u4ec5\u4ec5\u662f\u4e3a\u4e86\u6545\u5f04\u7384\u865a\u548c\u5176\u4ed6\u76ee\u7684\uff0c\u8fd9\u662f\u6240\u6709\u6620\u5c04\u4e2d\u6700\u5371\u9669\u7684\u3002reinterpret_cast \u5c31\u662f\u4e00\u628a\u9510\u5229\u65e0\u6bd4\u7684\u53cc\u5203\u5251\uff0c\u9664\u975e\u4f60\u5904\u4e8e\u80cc\u6c34\u4e00\u6218\u548c\u706b\u70e7\u7709\u6bdb\u7684\u5371\u6025\u65f6\u523b\uff0c\u5426\u5219\u7edd\u4e0d\u80fd\u4f7f\u7528\u3002<\/p>\n<p>\u5176\u5b9e <code>reinterpret_cast<\/code> \u7528\u6cd5\u7ec6\u8282\u8fd8\u6709\u4e0d\u5c11\uff0c\u4ec0\u4e48\u65f6\u5019\u9700\u8981\u7528\u5230\uff0c\u518d\u53bb\u5b98\u65b9\u4e86\u89e3\u4e00\u4e0b\u5c31\u597d\u4e86\uff0c\u73b0\u5728\u7ea0\u7684\u592a\u7ec6\u610f\u4e49\u4e0d\u5927\u3002 <img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/03\/20210302154706517.gifpic_center.png\" alt=\"C++ RTTI \u548c\u56db\u79cd\u7c7b\u578b\u8f6c\u6362\" \/><\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5419645\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chesterzzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bb2\u5f97\u633a\u597d\u7684\uff0c\u5df2\u5173\u6ce8\u516c\u4f17\u53f7                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5419646\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yiouejv <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chesterzzy \u563f\u563f\uff0c\u4e00\u8d77\u5b66\u4e60                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C++ RTTI \u548c\u56db\u79cd\u7c7b\u578b\u8f6c\u6362 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/362032"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=362032"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/362032\/revisions"}],"predecessor-version":[{"id":362049,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/362032\/revisions\/362049"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=362032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=362032"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=362032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}