{"id":1351,"date":"2018-06-07T23:04:59","date_gmt":"2018-06-07T15:04:59","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=1351"},"modified":"2018-06-07T23:04:59","modified_gmt":"2018-06-07T15:04:59","slug":"c%e4%b8%ad%e7%9a%84%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2static_cast%e3%80%81dynamic_cast%e3%80%81const_cast%e5%92%8creinterpret_cast%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/1351","title":{"rendered":"C++\u4e2d\u7684\u7c7b\u578b\u8f6c\u6362static_cast\u3001dynamic_cast\u3001const_cast\u548creinterpret_cast\u603b\u7ed3"},"content":{"rendered":"<div class=\"title\">\n<h1 class=\"YaHei\">C++\u4e2d\u7684\u7c7b\u578b\u8f6c\u6362static_cast\u3001dynamic_cast\u3001const_cast\u548creinterpret_cast\u603b\u7ed3<\/h1>\n<p><i>\u8f6c\u8f7d<\/i>\u00a0\u00a0\u66f4\u65b0\u65f6\u95f4\uff1a2014\u5e7410\u670801\u65e5 13:33:47 \u00a0 \u4f5c\u8005\uff1a\u679c\u51bb\u60f3 \u00a0\u00a0<a href=\"http:\/\/www.jb51.net\/article\/55885.htm#comments\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.jb51.net\/skin\/2018\/images\/text-message.png\" width=\"15\" height=\"15\" \/>\u00a0\u6211\u8981\u8bc4\u8bba<\/a><\/p>\n<\/div>\n<div class=\"lbd clearfix\"><\/div>\n<div class=\"art_desc mt10\">\n<div id=\"art_demo\">\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86C++\u4e2d\u7684\u7c7b\u578b\u8f6c\u6362static_cast\u3001dynamic_cast\u3001const_cast\u548creinterpret_cast\u603b\u7ed3,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/div>\n<\/div>\n<div class=\"lbd clearfix\"><\/div>\n<div id=\"content\">\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u603b\u7ed3\u7684\u662fC++\u4e2d\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u8fd9\u4e9b\u5c0f\u7684\u77e5\u8bc6\u70b9\uff0c\u6709\u7684\u65f6\u5019\uff0c\u81ea\u5df1\u4e0d\u662f\u5f88\u6ce8\u610f\uff0c\u4f46\u662f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u786e\u5b9e\u7ecf\u5e38\u4f7f\u7528\u7684\u3002\u4fd7\u8bdd\u8bf4\u7684\u597d\uff0c\u4e0d\u61c2\u81ea\u5df1\u5199\u7684\u4ee3\u7801\u7684\u7a0b\u5e8f\u5458\uff0c\u4e0d\u662f\u597d\u7684\u7a0b\u5e8f\u5458\uff1b\u5982\u679c\u4e00\u4e2a\u7a0b\u5e8f\u5458\u5bf9\u4e8e\u81ea\u5df1\u5199\u7684\u4ee3\u7801\u90fd\u4e0d\u61c2\uff0c\u53ea\u662f\u77e5\u9053\u4e00\u6627\u7684\u7684\u53bb\u4f7f\u7528\uff0c\u7ec8\u6709\u4e00\u5929\uff0c\u4f60\u4f1a\u8ff7\u5931\u4f60\u81ea\u5df1\u7684\u3002<\/p>\n<p>C++\u4e2d\u7684\u7c7b\u578b\u8f6c\u6362\u5206\u4e3a\u4e24\u79cd\uff1a<\/p>\n<p>1.\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff1b<br \/>\n2.\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p>\u800c\u5bf9\u4e8e\u9690\u5f0f\u53d8\u6362\uff0c\u5c31\u662f\u6807\u51c6\u7684\u8f6c\u6362\uff0c\u5728\u5f88\u591a\u65f6\u5019\uff0c\u4e0d\u7ecf\u610f\u95f4\u5c31\u53d1\u751f\u4e86\uff0c\u6bd4\u5982int\u7c7b\u578b\u548cfloat\u7c7b\u578b\u76f8\u52a0\u65f6\uff0cint\u7c7b\u578b\u5c31\u4f1a\u88ab\u9690\u5f0f\u7684\u8f6c\u6362\u4f4dfloat\u7c7b\u578b\uff0c\u7136\u540e\u518d\u8fdb\u884c\u76f8\u52a0\u8fd0\u7b97\u3002\u800c\u5173\u4e8e\u9690\u5f0f\u8f6c\u6362\u4e0d\u662f\u4eca\u5929\u603b\u7ed3\u7684\u91cd\u70b9\uff0c\u91cd\u70b9\u662f\u663e\u5f0f\u8f6c\u6362\u3002\u5728\u6807\u51c6C++\u4e2d\u6709\u56db\u4e2a\u7c7b\u578b\u8f6c\u6362\u7b26\uff1astatic_cast\u3001dynamic_cast\u3001const_cast\u548creinterpret_cast\uff1b\u4e0b\u9762\u5c06\u5bf9\u5b83\u4eec\u4e00\u4e00\u7684\u8fdb\u884c\u603b\u7ed3\u3002<\/p>\n<p><strong>static_cast<\/strong><\/p>\n<p>static_cast\u7684\u8f6c\u6362\u683c\u5f0f\uff1astatic_cast &lt;type-id&gt; (expression)<\/p>\n<p>\u5c06expression\u8f6c\u6362\u4e3atype-id\u7c7b\u578b\uff0c\u4e3b\u8981\u7528\u4e8e\u975e\u591a\u6001\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u4e0d\u63d0\u4f9b\u8fd0\u884c\u65f6\u7684\u68c0\u67e5\u6765\u786e\u4fdd\u8f6c\u6362\u7684\u5b89\u5168\u6027\u3002\u4e3b\u8981\u5728\u4ee5\u4e0b\u51e0\u79cd\u573a\u5408\u4e2d\u4f7f\u7528\uff1a<\/p>\n<p>1.\u7528\u4e8e\u7c7b\u5c42\u6b21\u7ed3\u6784\u4e2d\uff0c\u57fa\u7c7b\u548c\u5b50\u7c7b\u4e4b\u95f4\u6307\u9488\u548c\u5f15\u7528\u7684\u8f6c\u6362\uff1b<br \/>\n\u5f53\u8fdb\u884c\u4e0a\u884c\u8f6c\u6362\uff0c\u4e5f\u5c31\u662f\u628a\u5b50\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u8f6c\u6362\u6210\u7236\u7c7b\u8868\u793a\uff0c\u8fd9\u79cd\u8f6c\u6362\u662f\u5b89\u5168\u7684\uff1b<br \/>\n\u5f53\u8fdb\u884c\u4e0b\u884c\u8f6c\u6362\uff0c\u4e5f\u5c31\u662f\u628a\u7236\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u8f6c\u6362\u6210\u5b50\u7c7b\u8868\u793a\uff0c\u8fd9\u79cd\u8f6c\u6362\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u4e5f\u9700\u8981\u7a0b\u5e8f\u5458\u6765\u4fdd\u8bc1\uff1b<\/p>\n<p>2.\u7528\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u5982\u628aint\u8f6c\u6362\u6210char\uff0c\u628aint\u8f6c\u6362\u6210enum\u7b49\u7b49\uff0c\u8fd9\u79cd\u8f6c\u6362\u7684\u5b89\u5168\u6027\u9700\u8981\u7a0b\u5e8f\u5458\u6765\u4fdd\u8bc1\uff1b<\/p>\n<p>3.\u628avoid\u6307\u9488\u8f6c\u6362\u6210\u76ee\u6807\u7c7b\u578b\u7684\u6307\u9488\uff0c\u662f\u53ca\u5176\u4e0d\u5b89\u5168\u7684\uff1b<\/p>\n<p>\u6ce8\uff1astatic_cast\u4e0d\u80fd\u8f6c\u6362\u6389expression\u7684const\u3001volatile\u548c__unaligned\u5c5e\u6027\u3002<\/p>\n<p><strong>dynamic_cast<\/strong><\/p>\n<p>dynamic_cast\u7684\u8f6c\u6362\u683c\u5f0f\uff1adynamic_cast &lt;type-id&gt; (expression)<\/p>\n<p>\u5c06expression\u8f6c\u6362\u4e3atype-id\u7c7b\u578b\uff0ctype-id\u5fc5\u987b\u662f\u7c7b\u7684\u6307\u9488\u3001\u7c7b\u7684\u5f15\u7528\u6216\u8005\u662fvoid *\uff1b\u5982\u679ctype-id\u662f\u6307\u9488\u7c7b\u578b\uff0c\u90a3\u4e48expression\u4e5f\u5fc5\u987b\u662f\u4e00\u4e2a\u6307\u9488\uff1b\u5982\u679ctype-id\u662f\u4e00\u4e2a\u5f15\u7528\uff0c\u90a3\u4e48expression\u4e5f\u5fc5\u987b\u662f\u4e00\u4e2a\u5f15\u7528\u3002<\/p>\n<p>dynamic_cast\u4e3b\u8981\u7528\u4e8e\u7c7b\u5c42\u6b21\u95f4\u7684\u4e0a\u884c\u8f6c\u6362\u548c\u4e0b\u884c\u8f6c\u6362\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u7c7b\u4e4b\u95f4\u7684\u4ea4\u53c9\u8f6c\u6362\u3002\u5728\u7c7b\u5c42\u6b21\u95f4\u8fdb\u884c\u4e0a\u884c\u8f6c\u6362\u65f6\uff0cdynamic_cast\u548cstatic_cast\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\uff1b\u5728\u8fdb\u884c\u4e0b\u884c\u8f6c\u6362\u65f6\uff0cdynamic_cast\u5177\u6709\u7c7b\u578b\u68c0\u67e5\u7684\u529f\u80fd\uff0c\u6bd4static_cast\u66f4\u5b89\u5168\u3002\u5728\u591a\u6001\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u4e3b\u8981\u4f7f\u7528dynamic_cast\uff0c\u56e0\u4e3a\u7c7b\u578b\u63d0\u4f9b\u4e86\u8fd0\u884c\u65f6\u4fe1\u606f\u3002\u4e0b\u9762\u6211\u5c06\u5206\u522b\u5728\u4ee5\u4e0b\u7684\u51e0\u79cd\u573a\u5408\u4e0b\u8fdb\u884cdynamic_cast\u7684\u4f7f\u7528\u603b\u7ed3\uff1a<\/p>\n<p>1.\u6700\u7b80\u5355\u7684\u4e0a\u884c\u8f6c\u6362<\/p>\n<p>\u6bd4\u5982B\u7ee7\u627f\u81eaA\uff0cB\u8f6c\u6362\u4e3aA\uff0c\u8fdb\u884c\u4e0a\u884c\u8f6c\u6362\u65f6\uff0c\u662f\u5b89\u5168\u7684\uff0c\u5982\u4e0b\uff1a<\/p>\n<div class=\"codetitle\"><a id=\"copybut24389\" class=\"copybut\"><\/a><u>\u590d\u5236\u4ee3\u7801<\/u>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code24389\" class=\"codebody\">\n#include &lt;iostream&gt;<br \/>\nusing namespace std;<br \/>\nclass A<br \/>\n{<br \/>\n\/\/ &#8230;&#8230;<br \/>\n};<br \/>\nclass B : public A<br \/>\n{<br \/>\n\/\/ &#8230;&#8230;<br \/>\n};<br \/>\nint main()<br \/>\n{<br \/>\nB *pB = new B;<br \/>\nA *pA = dynamic_cast&lt;A *&gt;(pB); \/\/ Safe and will succeed<br \/>\n}<\/div>\n<p>2.\u591a\u91cd\u7ee7\u627f\u4e4b\u95f4\u7684\u4e0a\u884c\u8f6c\u6362<\/p>\n<p>C\u7ee7\u627f\u81eaB\uff0cB\u7ee7\u627f\u81eaA\uff0c\u8fd9\u79cd\u591a\u91cd\u7ee7\u627f\u7684\u5173\u7cfb\uff1b\u4f46\u662f\uff0c\u5173\u7cfb\u5f88\u660e\u786e\uff0c\u4f7f\u7528dynamic_cast\u8fdb\u884c\u8f6c\u6362\u65f6\uff0c\u4e5f\u662f\u5f88\u7b80\u5355\u7684\uff1a<\/p>\n<div class=\"codetitle\"><a id=\"copybut16103\" class=\"copybut\"><\/a><u>\u590d\u5236\u4ee3\u7801<\/u>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code16103\" class=\"codebody\">\nclass A<br \/>\n{<br \/>\n\/\/ &#8230;&#8230;<br \/>\n};<br \/>\nclass B : public A<br \/>\n{<br \/>\n\/\/ &#8230;&#8230;<br \/>\n};<br \/>\nclass C : public B<br \/>\n{<br \/>\n\/\/ &#8230;&#8230;<br \/>\n};<br \/>\nint main()<br \/>\n{<br \/>\nC *pC = new C;<br \/>\nB *pB = dynamic_cast&lt;B *&gt;(pC); \/\/ OK<br \/>\nA *pA = dynamic_cast&lt;A *&gt;(pC); \/\/ OK<br \/>\n}<\/div>\n<p>\u800c\u4e0a\u8ff0\u7684\u8f6c\u6362\uff0cstatic_cast\u548cdynamic_cast\u5177\u6709\u540c\u6837\u7684\u6548\u679c\u3002\u800c\u8fd9\u79cd\u4e0a\u884c\u8f6c\u6362\uff0c\u4e5f\u88ab\u79f0\u4e3a\u9690\u5f0f\u8f6c\u6362\uff1b\u6bd4\u5982\u6211\u4eec\u5728\u5b9a\u4e49\u53d8\u91cf\u65f6\u7ecf\u5e38\u8fd9\u4e48\u5199\uff1aB *pB = new C;\u8fd9\u548c\u4e0a\u9762\u662f\u4e00\u4e2a\u9053\u7406\u7684\uff0c\u53ea\u662f\u591a\u52a0\u4e86\u4e00\u4e2adynamic_cast\u8f6c\u6362\u7b26\u800c\u5df2\u3002<\/p>\n<p>3.\u8f6c\u6362\u6210void *<\/p>\n<p>\u53ef\u4ee5\u5c06\u7c7b\u8f6c\u6362\u6210void *\uff0c\u4f8b\u5982\uff1a<\/p>\n<div class=\"codetitle\"><a id=\"copybut94077\" class=\"copybut\"><\/a><u>\u590d\u5236\u4ee3\u7801<\/u>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code94077\" class=\"codebody\">\nclass A<br \/>\n{<br \/>\npublic:<br \/>\nvirtual void f(){}<br \/>\n\/\/ &#8230;&#8230;<br \/>\n};<br \/>\nclass B<br \/>\n{<br \/>\npublic:<br \/>\nvirtual void f(){}<br \/>\n\/\/ &#8230;&#8230;<br \/>\n};<br \/>\nint main()<br \/>\n{<br \/>\nA *pA = new A;<br \/>\nB *pB = new B;<br \/>\nvoid *pV = dynamic_cast&lt;void *&gt;(pA); \/\/ pV points to an object of A<br \/>\npV = dynamic_cast&lt;void *&gt;(pB); \/\/ pV points to an object of B<br \/>\n}<\/div>\n<p>\u4f46\u662f\uff0c\u5728\u7c7bA\u548c\u7c7bB\u4e2d\u5fc5\u987b\u5305\u542b\u865a\u51fd\u6570\uff0c\u4e3a\u4ec0\u4e48\u5462\uff1f\u56e0\u4e3a\u7c7b\u4e2d\u5b58\u5728\u865a\u51fd\u6570\uff0c\u5c31\u8bf4\u660e\u5b83\u6709\u60f3\u8ba9\u57fa\u7c7b\u6307\u9488\u6216\u5f15\u7528\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\u7684\u60c5\u51b5\uff0c\u6b64\u65f6\u8f6c\u6362\u624d\u6709\u610f\u4e49\uff1b\u7531\u4e8e\u8fd0\u884c\u65f6\u7c7b\u578b\u68c0\u67e5\u9700\u8981\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f\uff0c\u800c\u8fd9\u4e2a\u4fe1\u606f\u5b58\u50a8\u5728\u7c7b\u7684\u865a\u51fd\u6570\u8868\u4e2d\uff0c\u53ea\u6709\u5b9a\u4e49\u4e86\u865a\u51fd\u6570\u7684\u7c7b\u624d\u6709\u865a\u51fd\u6570\u8868\u3002<\/p>\n<p>4.\u5982\u679cexpression\u662ftype-id\u7684\u57fa\u7c7b\uff0c\u4f7f\u7528dynamic_cast\u8fdb\u884c\u8f6c\u6362\u65f6\uff0c\u5728\u8fd0\u884c\u65f6\u5c31\u4f1a\u68c0\u67e5expression\u662f\u5426\u771f\u6b63\u7684\u6307\u5411\u4e00\u4e2atype-id\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5982\u679c\u662f\uff0c\u5219\u80fd\u8fdb\u884c\u6b63\u786e\u7684\u8f6c\u6362\uff0c\u83b7\u5f97\u5bf9\u5e94\u7684\u503c\uff1b\u5426\u5219\u8fd4\u56deNULL\uff0c\u5982\u679c\u662f\u5f15\u7528\uff0c\u5219\u5728\u8fd0\u884c\u65f6\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38\uff1b\u4f8b\u5982\uff1a<\/p>\n<div class=\"codetitle\"><a id=\"copybut33597\" class=\"copybut\"><\/a><u>\u590d\u5236\u4ee3\u7801<\/u>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code33597\" class=\"codebody\">\nclass B<br \/>\n{<br \/>\nvirtual void f(){};<br \/>\n};<br \/>\nclass D : public B<br \/>\n{<br \/>\nvirtual void f(){};<br \/>\n};<br \/>\nvoid main()<br \/>\n{<br \/>\nB* pb = new D;\u00a0\u00a0 \/\/ unclear but ok<br \/>\nB* pb2 = new B;<br \/>\nD* pd = dynamic_cast&lt;D*&gt;(pb);\u00a0\u00a0 \/\/ ok: pb actually points to a D<br \/>\nD* pd2 = dynamic_cast&lt;D*&gt;(pb2);\u00a0\u00a0 \/\/ pb2 points to a B not a D, now pd2 is NULL<br \/>\n}<\/div>\n<p>\u8fd9\u4e2a\u5c31\u662f\u4e0b\u884c\u8f6c\u6362\uff0c\u4ece\u57fa\u7c7b\u6307\u9488\u8f6c\u6362\u5230\u6d3e\u751f\u7c7b\u6307\u9488\u3002<br \/>\n\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u7ee7\u627f\u5173\u7cfb\u6765\u8bf4\uff0c\u4f7f\u7528dynamic_cast\u8fdb\u884c\u8f6c\u6362\u662f\u5b58\u5728\u4e00\u4e9b\u9677\u9631\u7684\uff1b\u6bd4\u5982\uff0c\u6709\u5982\u4e0b\u7684\u4e00\u4e2a\u7ed3\u6784\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/files.jb51.net\/file_images\/article\/201410\/2014101132055194.png?201491132113\" alt=\"\" \/><\/p>\n<p>D\u7c7b\u578b\u53ef\u4ee5\u5b89\u5168\u7684\u8f6c\u6362\u6210B\u548cC\u7c7b\u578b\uff0c\u4f46\u662fD\u7c7b\u578b\u8981\u662f\u76f4\u63a5\u8f6c\u6362\u6210A\u7c7b\u578b\u5462\uff1f<\/p>\n<div class=\"codetitle\"><a id=\"copybut15035\" class=\"copybut\"><\/a><u>\u590d\u5236\u4ee3\u7801<\/u>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code15035\" class=\"codebody\">\nclass A<br \/>\n{<br \/>\nvirtual void Func() = 0;<br \/>\n};<br \/>\nclass B : public A<br \/>\n{<br \/>\nvoid Func(){};<br \/>\n};<br \/>\nclass C : public A<br \/>\n{<br \/>\nvoid Func(){};<br \/>\n};<br \/>\nclass D : public B, public C<br \/>\n{<br \/>\nvoid Func(){}<br \/>\n};<br \/>\nint main()<br \/>\n{<br \/>\nD *pD = new D;<br \/>\nA *pA = dynamic_cast&lt;A *&gt;(pD); \/\/ You will get a pA which is NULL<br \/>\n}<\/div>\n<p>\u5982\u679c\u8fdb\u884c\u4e0a\u9762\u7684\u76f4\u63a5\u8f6c\uff0c\u4f60\u5c06\u4f1a\u5f97\u5230\u4e00\u4e2aNULL\u7684pA\u6307\u9488\uff1b\u8fd9\u662f\u56e0\u4e3a\uff0cB\u548cC\u90fd\u7ee7\u627f\u4e86A\uff0c\u5e76\u4e14\u90fd\u5b9e\u73b0\u4e86\u865a\u51fd\u6570Func\uff0c\u5bfc\u81f4\u5728\u8fdb\u884c\u8f6c\u6362\u65f6\uff0c\u65e0\u6cd5\u8fdb\u884c\u6289\u62e9\u5e94\u8be5\u5411\u54ea\u4e2aA\u8fdb\u884c\u8f6c\u6362\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\uff1a<\/p>\n<div class=\"codetitle\"><a id=\"copybut97281\" class=\"copybut\"><\/a><u>\u590d\u5236\u4ee3\u7801<\/u>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code97281\" class=\"codebody\">\nint main()<br \/>\n{<br \/>\nD *pD = new D;<br \/>\nB *pB = dynamic_cast&lt;B *&gt;(pD);<br \/>\nA *pA = dynamic_cast&lt;A *&gt;(pB);<br \/>\n}<\/div>\n<p>\u8fd9\u5c31\u662f\u6211\u5728\u5b9e\u73b0QueryInterface\u65f6\uff0c\u5f97\u5230IUnknown\u7684\u6307\u9488\u65f6\uff0c\u4f7f\u7528\u7684\u662f*ppv = static_cast&lt;IX *&gt;(this);\u800c\u4e0d\u662f*ppv = static_cast&lt;IUnknown *&gt;(this);<\/p>\n<p>\u5bf9\u4e8e\u591a\u91cd\u7ee7\u627f\u7684\u60c5\u51b5\uff0c\u4ece\u6d3e\u751f\u7c7b\u5f80\u7236\u7c7b\u7684\u7236\u7c7b\u8fdb\u884c\u8f6c\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\uff1b\u6bd4\u5982\u6709\u4e0b\u9762\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/files.jb51.net\/file_images\/article\/201410\/2014101132148372.png?201491132156\" alt=\"\" \/><\/p>\n<p>\u73b0\u5728\uff0c\u4f60\u62e5\u6709\u4e00\u4e2aA\u7c7b\u578b\u7684\u6307\u9488\uff0c\u5b83\u6307\u5411E\u5b9e\u4f8b\uff0c\u5982\u4f55\u83b7\u5f97B\u7c7b\u578b\u7684\u6307\u9488\uff0c\u6307\u5411E\u5b9e\u4f8b\u5462\uff1f\u5982\u679c\u76f4\u63a5\u8fdb\u884c\u8f6c\u7684\u8bdd\uff0c\u5c31\u4f1a\u51fa\u73b0\u7f16\u8bd1\u5668\u51fa\u73b0\u5206\u6b67\uff0c\u4e0d\u77e5\u9053\u662f\u8d70E-&gt;C-&gt;B\uff0c\u8fd8\u662f\u8d70E-&gt;D-&gt;B\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u5148\u5c06A\u7c7b\u578b\u7684\u6307\u9488\u8fdb\u884c\u4e0b\u884c\u8f6c\u6362\uff0c\u83b7\u5f97E\u7c7b\u578b\u7684\u6307\u9488\uff0c\u7136\u540e\uff0c\u5728\u6307\u5b9a\u4e00\u6761\u6b63\u786e\u7684\u8def\u7ebf\u8fdb\u884c\u4e0a\u884c\u8f6c\u6362\u3002<\/p>\n<p>\u4e0a\u9762\u5c31\u662f\u5bf9\u4e8edynamic_cast\u8f6c\u6362\u7684\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u591a\u91cd\u7ee7\u627f\u7684\u60c5\u51b5\uff0c\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5f88\u5bb9\u6613\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<p><strong>const_cast<\/strong><\/p>\n<p>const_cast\u7684\u8f6c\u6362\u683c\u5f0f\uff1aconst_cast &lt;type-id&gt; (expression)<\/p>\n<p>const_cast\u7528\u6765\u5c06\u7c7b\u578b\u7684const\u3001volatile\u548c__unaligned\u5c5e\u6027\u79fb\u9664\u3002\u5e38\u91cf\u6307\u9488\u88ab\u8f6c\u6362\u6210\u975e\u5e38\u91cf\u6307\u9488\uff0c\u5e76\u4e14\u4ecd\u7136\u6307\u5411\u539f\u6765\u7684\u5bf9\u8c61\uff1b\u5e38\u91cf\u5f15\u7528\u88ab\u8f6c\u6362\u6210\u975e\u5e38\u91cf\u5f15\u7528\uff0c\u5e76\u4e14\u4ecd\u7136\u5f15\u7528\u539f\u6765\u7684\u5bf9\u8c61\u3002\u770b\u4ee5\u4e0b\u7684\u4ee3\u7801\u4f8b\u5b50\uff1a<\/p>\n<div class=\"codetitle\"><a id=\"copybut23984\" class=\"copybut\"><\/a><u>\u590d\u5236\u4ee3\u7801<\/u>\u4ee3\u7801\u5982\u4e0b:<\/div>\n<div id=\"code23984\" class=\"codebody\">\n\/*<br \/>\n** FileName\u00a0\u00a0\u00a0\u00a0 : ConstCastDemo<br \/>\n** Author\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : Jelly Young<br \/>\n** Date\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 2013\/12\/27<br \/>\n** Description\u00a0 : More information, please go to http:\/\/www.jb51.net<br \/>\n*\/<br \/>\n#include &lt;iostream&gt;<br \/>\nusing namespace std;<br \/>\nclass CA<br \/>\n{<br \/>\npublic:<br \/>\nCA():m_iA(10){}<br \/>\nint m_iA;<br \/>\n};<br \/>\nint main()<br \/>\n{<br \/>\nconst CA *pA = new CA;<br \/>\n\/\/ pA-&gt;m_iA = 100; \/\/ Error<br \/>\nCA *pB = const_cast&lt;CA *&gt;(pA);<br \/>\npB-&gt;m_iA = 100;<br \/>\n\/\/ Now the pA and the pB points to the same object<br \/>\ncout&lt;&lt;pA-&gt;m_iA&lt;&lt;endl;<br \/>\ncout&lt;&lt;pB-&gt;m_iA&lt;&lt;endl;<br \/>\nconst CA &amp;a = *pA;<br \/>\n\/\/ a.m_iA = 200; \/\/ Error<br \/>\nCA &amp;b = const_cast&lt;CA &amp;&gt;(a);<br \/>\npB-&gt;m_iA = 200;<br \/>\n\/\/ Now the a and the b reference to the same object<br \/>\ncout&lt;&lt;b.m_iA&lt;&lt;endl;<br \/>\ncout&lt;&lt;a.m_iA&lt;&lt;endl;<br \/>\n}<\/div>\n<p>\u6ce8\uff1a\u4f60\u4e0d\u80fd\u76f4\u63a5\u5bf9\u975e\u6307\u9488\u548c\u975e\u5f15\u7528\u7684\u53d8\u91cf\u4f7f\u7528const_cast\u64cd\u4f5c\u7b26\u53bb\u76f4\u63a5\u79fb\u9664\u5b83\u7684const\u3001volatile\u548c__unaligned\u5c5e\u6027\u3002<\/p>\n<p><strong>reinterpret_cast<\/strong><\/p>\n<p>reinterpret_cast\u7684\u8f6c\u6362\u683c\u5f0f\uff1areinterpret_cast &lt;type-id&gt; (expression)<\/p>\n<p>\u5141\u8bb8\u5c06\u4efb\u4f55\u6307\u9488\u7c7b\u578b\u8f6c\u6362\u4e3a\u5176\u5b83\u7684\u6307\u9488\u7c7b\u578b\uff1b\u542c\u8d77\u6765\u5f88\u5f3a\u5927\uff0c\u4f46\u662f\u4e5f\u5f88\u4e0d\u9760\u8c31\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u4ece\u4e00\u79cd\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\u3002\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6307\u9488\u8f6c\u6362\u6210\u4e00\u4e2a\u6574\u6570\uff0c\u4e5f\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6574\u6570\u8f6c\u6362\u6210\u4e00\u4e2a\u6307\u9488\uff0c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5148\u628a\u4e00\u4e2a\u6307\u9488\u8f6c\u6362\u6210\u4e00\u4e2a\u6574\u6570\uff0c\u5728\u628a\u8be5\u6574\u6570\u8f6c\u6362\u6210\u539f\u7c7b\u578b\u7684\u6307\u9488\uff0c\u8fd8\u53ef\u4ee5\u5f97\u5230\u539f\u6765\u7684\u6307\u9488\u503c\uff1b\u7279\u522b\u662f\u5f00\u8f9f\u4e86\u7cfb\u7edf\u5168\u5c40\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u9700\u8981\u5728\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u4f7f\u7528\u65f6\uff0c\u9700\u8981\u5f7c\u6b64\u5171\u4eab\uff0c\u4f20\u9012\u8fd9\u4e2a\u5185\u5b58\u7a7a\u95f4\u7684\u6307\u9488\u65f6\uff0c\u5c31\u53ef\u4ee5\u5c06\u6307\u9488\u8f6c\u6362\u6210\u6574\u6570\u503c\uff0c\u5f97\u5230\u4ee5\u540e\uff0c\u518d\u5c06\u6574\u6570\u503c\u8f6c\u6362\u6210\u6307\u9488\uff0c\u8fdb\u884c\u5bf9\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u8fd9\u7bc7\u535a\u6587\u603b\u7ed3\u4e86C++\u4e2d\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u91cd\u70b9\u603b\u7ed3\u4e86\u5176\u4e2d\u7684\u663e\u5f0f\u8f6c\u6362\u3002\u5bf9\u4e8eC++\u652f\u6301\u7684\u8fd9\u56db\u79cd\u663e\u5f0f\u8f6c\u6362\u90fd\u8fdb\u884c\u4e86\u8be6\u7ec6\u7684\u63cf\u8ff0\u3002\u5982\u679c\u5927\u5bb6\u6709\u4ec0\u4e48\u8865\u5145\u7684\uff0c\u6216\u8005\u6211\u603b\u7ed3\u7684\u6709\u8bef\u7684\u5730\u65b9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u6307\u6559\u3002<\/p>\n<div class=\"art_xg\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>C++\u4e2d\u7684\u7c7b\u578b\u8f6c\u6362static_cast<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-1351","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=1351"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1351\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}