{"id":371,"date":"2020-11-03T12:20:43","date_gmt":"2020-11-03T16:20:43","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/?page_id=371"},"modified":"2020-11-03T12:20:43","modified_gmt":"2020-11-03T16:20:43","slug":"advanced-class-type-casting","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/cplusplus-tutorial\/advanced-concepts\/advanced-class-type-casting","title":{"rendered":"\u7c7b\u578b\u8f6c\u6362\u9ad8\u7ea7"},"content":{"rendered":"\n<h2>5.4 \u7c7b\u578b\u8f6c\u6362\u9ad8\u7ea7 (Advacned Class Type-casting)<\/h2>\n\n\n\n<p>\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u4e00\u76f4\u4f7f\u7528\u4f20\u7edf\u7684\u7c7b\u578b\u8f6c\u6362\u7b26\u6765\u8fdb\u884c\u7b80\u5355\u5bf9\u8c61\u7684\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u8981\u628a\u4e00\u4e2adouble\u7c7b\u578b\u7684\u6d6e\u70b9\u578b\u6570\u5b57\u8f6c\u6362\u4e3aint \u7684\u6574\u578b\u6570\u5b57\uff0c\u6211\u4eec\u662f\u8fd9\u6837\u505a\u7684\uff1a<code>int i;<br>double d;<br>i = (int) d;<\/code><\/p>\n\n\n\n<p>\u6216\u8005<code>i = int (d);<\/code><\/p>\n\n\n\n<p>\u8fd9\u6837\u505a\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\u6ca1\u95ee\u9898\u7684\uff0c\u56e0\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u5df2\u7ecf\u6709\u6807\u51c6\u7684\u5b9a\u4e49\u3002\u540c\u6837\u7684\u64cd\u4f5c\u4e5f\u53ef\u4ee5\u88ab\u5728\u7c7b\u6216\u7c7b\u7684\u6307\u9488\u4e0a\uff0c\u56e0\u6b64\u4ee5\u4e0b\u4f8b\u5b50\u4e2d\u7684\u5199\u6cd5\u4e5f\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ class type-casting #include &lt;iostream.h&gt; class CDummy { int i; }; class CAddition { int x,y; public: CAddition (int a, int b) { x=a; y=b; } int result() { return x+y;} }; int main () { CDummy d; CAddition * padd; padd = (CAddition*) &amp;d; cout &lt;&lt; padd-&gt;result(); return 0; }<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u867d\u7136\u4ee5\u4e0a\u7a0b\u5e8f\u5728C++\u4e2d\u662f\u6ca1\u6709\u8bed\u6cd5\u9519\u8bef\u7684(\u591a\u6570\u7f16\u8bd1\u5668\u751a\u81f3\u4e0d\u4f1a\u4ea7\u751f\u8b66\u544a\u4fe1\u606f)\uff0c\u4f46\u8fd9\u6bb5\u7a0b\u5e8f\u6ca1\u6709\u4ec0\u4e48\u5b9e\u9645\u7684\u903b\u8f91\u610f\u4e49\u3002\u6211\u4eec\u4f7f\u7528\u4e86CAddition \u7684\u6210\u5458\u51fd\u6570result \u800c\u6ca1\u6709\u5b9a\u4e49\u4e00\u4e2a\u76f8\u5e94\u7684\u8be5\u7c7b\u7684\u5bf9\u8c61\uff1apadd \u5e76\u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u88ab\u6211\u4eec\u8d4b\u503c\u6307\u5411\u4e00\u4e2a\u6beb\u65e0\u5173\u7cfb\u7684\u5bf9\u8c61\u7684\u5730\u5740\u3002\u5f53\u5728\u7a0b\u5e8f\u8fd0\u884c\u5230\u8bbf\u95ee\u5b83\u7684result \u6210\u5458\u51fd\u6570\u65f6\uff0c\u5c06\u4f1a\u6709\u4e00\u4e2a\u8fd0\u884c\u9519\u8bef(run-time error)\u4ea7\u751f\uff0c\u6216\u751f\u6210\u4e00\u4e2a\u610f\u5916\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u63a7\u5236\u8fd9\u79cd\u7c7b\u4e4b\u95f4\u7684\u8f6c\u6362\uff0cANSI-C++ \u6807\u51c6\u5b9a\u4e49\u4e864\u79cd\u65b0\u7684\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26\uff1a reinterpret_cast, static_cast, dynamic_cast \u548c const_cast\u3002\u6240\u6709\u8fd9\u4e9b\u64cd\u4f5c\u7b26\u90fd\u662f\u540c\u6837\u7684\u4f7f\u7528\u683c\u5f0f\uff1areinterpret_cast &lt;new_type&gt; (expression)<br>dynamic_cast &lt;new_type&gt; (expression)<br>static_cast &lt;new_type&gt; (expression)<br>const_cast &lt;new_type&gt; (expression)<\/p>\n\n\n\n<p>\u8fd9\u91ccnew_type \u662f\u8981\u8f6c\u6362\u6210\u7684\u76ee\u6807\u7c7b\u578b\uff0cexpression \u662f\u8981\u88ab\u8f6c\u6362\u7684\u5185\u5bb9\u3002\u4e3a\u4e86\u4fbf\u4e8e\u7406\u89e3\uff0c\u6a21\u4eff\u4f20\u7edf\u8f6c\u6362\u64cd\u4f5c\u7b26\uff0c\u5b83\u4eec\u7684\u542b\u4e49\u662f\u8fd9\u6837\u7684\uff1a<code>(new_type) expression<br>new_type (expression)<\/code><\/p>\n\n\n\n<h3>reinterpret_cast<\/h3>\n\n\n\n<p>reinterpret_cast \u53ef\u4ee5\u5c06\u4e00\u4e2a\u6307\u9488\u8f6c\u6362\u4e3a\u4efb\u610f\u5176\u5b83\u7c7b\u578b\u7684\u6307\u9488\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u6765\u5c06\u4e00\u4e2a\u6307\u9488\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6574\u578b\uff0c\u6216\u53cd\u4e4b\u4ea6\u7136\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u64cd\u4f5c\u7b26\u53ef\u4ee5\u5728\u4e92\u4e0d\u76f8\u5173\u7684\u7c7b\u4e4b\u95f4\u8fdb\u884c\u6307\u9488\u8f6c\u6362\uff0c\u64cd\u4f5c\u7684\u7ed3\u679c\u662f\u7b80\u5355\u7684\u5c06\u4e00\u4e2a\u6307\u9488\u7684\u4e8c\u8fdb\u5236\u6570\u636e(binary copy)\u590d\u5236\u5230\u53e6\u4e00\u4e2a\u6307\u9488\u3002\u5bf9\u6307\u9488\u6307\u5411\u7684\u5185\u5bb9\u4e0d\u505a\u4efb\u4f55\u68c0\u67e5\u6216\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8fd9\u79cd\u590d\u5236\u53d1\u751f\u5728\u4e00\u4e2a\u6307\u9488\u5230\u4e00\u4e2a\u6574\u6570\u4e4b\u95f4\uff0c\u5219\u5bf9\u5176\u5185\u5bb9\u7684\u89e3\u91ca\u53d6\u51b3\u4e8e\u4e0d\u540c\u7684\u7cfb\u7edf\uff0c\u56e0\u6b64\u4efb\u4f55\u5b9e\u73b0\u90fd\u662f\u4e0d\u53ef\u79fb\u690d(non portable)\u7684\u3002\u4e00\u4e2a\u6307\u9488\u5982\u679c\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a\u80fd\u591f\u5b8c\u5168\u5b58\u50a8\u5b83\u7684\u8db3\u591f\u5927\u7684\u6574\u6570\u4e2d\uff0c\u5219\u662f\u53ef\u4ee5\u518d\u88ab\u8f6c\u6362\u56de\u6765\u6210\u4e3a\u6307\u9488\u7684\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1aclass A {};<br>class B {};<br>A * a = new A;<br>B * b = reinterpret_cast&lt;B*&gt;(a);<\/p>\n\n\n\n<p>reinterpret_cast \u5bf9\u6240\u6709\u6307\u9488\u7684\u5904\u7406\u4e0e\u4f20\u7edf\u7684\u7c7b\u578b\u8f6c\u6362\u7b26\u6240\u4f5c\u7684\u4e00\u6a21\u4e00\u6837\u3002<\/p>\n\n\n\n<h3>static_cast<\/h3>\n\n\n\n<p>static_cast \u53ef\u4ee5\u6267\u884c\u6240\u6709\u80fd\u591f\u9690\u542b\u6267\u884c\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u4ee5\u53ca\u5b83\u4eec\u7684\u53cd\u5411\u64cd\u4f5c\uff08\u5373\u4f7f\u8fd9\u79cd\u65b9\u5411\u64cd\u4f5c\u662f\u4e0d\u5141\u8bb8\u9690\u542b\u6267\u884c\u7684\uff09\u3002<\/p>\n\n\n\n<p>\u7528\u4e8e\u7c7b\u7684\u6307\u9488\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u5141\u8bb8\u5c06\u4e00\u4e2a\u5f15\u7533\u7c7b\u7684\u6307\u9488\u8f6c\u6362\u4e3a\u5176\u57fa\u7c7b\u7c7b\u578b\uff08\u8fd9\u662f\u53ef\u4ee5\u88ab\u9690\u542b\u6267\u884c\u7684\u6709\u6548\u8f6c\u6362\uff09\uff0c\u540c\u65f6\u4e5f\u5141\u8bb8\u8fdb\u884c\u76f8\u53cd\u7684\u8f6c\u6362\uff1a\u5c06\u4e00\u4e2a\u57fa\u7c7b\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5f15\u7533\u7c7b\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u5728\u540e\u9762\u4e00\u79cd\u60c5\u51b5\u4e2d\uff0c\u4e0d\u4f1a\u68c0\u67e5\u88ab\u8f6c\u6362\u7684\u57fa\u7c7b\u662f\u5426\u771f\u6b63\u5b8c\u5168\u662f\u76ee\u6807\u7c7b\u578b\u7684\u3002\u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u5408\u6cd5\u7684\uff1aclass Base {};<br>class Derived: public Base {};<br>Base * a = new Base;<br>Derived * b = static_cast(a);<\/p>\n\n\n\n<p>static_cast\u9664\u4e86\u80fd\u591f\u5bf9\u7c7b\u6307\u9488\u8fdb\u884c\u64cd\u4f5c\uff0c\u8fd8\u53ef\u4ee5\u88ab\u7528\u6765\u8fdb\u884c\u7c7b\u4e2d\u660e\u786e\u5b9a\u4e49\u7684\u8f6c\u6362\uff0c\u4ee5\u53ca\u5bf9\u57fa\u672c\u7c7b\u578b\u7684\u6807\u51c6\u8f6c\u6362\uff1a<code>double d=3.14159265;<br>int i = static_cast&lt;int&gt;(d);<\/code><\/p>\n\n\n\n<p>\u8bd1\u8005\u6ce8\uff1a\u5982\u679c\u4f60\u5bf9\u8fd9\u90e8\u5206\u770b\u4e0d\u592a\u61c2\uff0c\u8bf7\u7ed3\u5408\u4e0b\u9762\u7684dynamic_cast\u4e00\u8d77\u770b\uff0c\u4e5f\u8bb8\u4f1a\u5e2e\u52a9\u7406\u89e3\u3002<\/p>\n\n\n\n<h3>dynamic_cast<\/h3>\n\n\n\n<p>dynamic_cast \u5b8c\u5168\u88ab\u7528\u6765\u8fdb\u884c\u6307\u9488\u7684\u64cd\u4f5c\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u4efb\u4f55\u53ef\u4ee5\u9690\u542b\u8fdb\u884c\u7684\u8f6c\u6362\u64cd\u4f5c\u4ee5\u53ca\u5b83\u4eec\u88ab\u7528\u4e8e\u591a\u6001\u7c7b\u60c5\u51b5\u4e0b\u7684\u65b9\u5411\u64cd\u4f5c\u3002\u7136\u800c\u4e0estatic_cast\u4e0d\u540c\u7684\u662f\uff0c dynamic_cast \u4f1a\u68c0\u67e5\u540e\u4e00\u79cd\u60c5\u51b5\u7684\u64cd\u4f5c\u662f\u5426\u5408\u6cd5\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u4f1a\u68c0\u67e5\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u662f\u5426\u4f1a\u8fd4\u56de\u4e00\u4e2a\u88ab\u8981\u6c42\u7c7b\u578b\u7684\u6709\u6548\u7684\u5b8c\u6574\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u68c0\u67e5\u662f\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u7684\u3002\u5982\u679c\u88ab\u8f6c\u6362\u7684\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61\u4e0d\u662f\u4e00\u4e2a\u88ab\u8981\u6c42\u7c7b\u578b\u7684\u6709\u6548\u5b8c\u6574\u7684\u5bf9\u8c61\uff0c\u8fd4\u56de\u503c\u5c06\u4f1a\u662f\u4e00\u4e2a\u7a7a\u6307\u9488NULL \u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   class Base { virtual dummy(){}; };<br>   class Derived : public Base { };<br>   <br><br>   Base* b1 = new Derived;<br>   Base* b2 = new Base;<br>   Derived* d1 = dynamic_cast(b1);   \/\/ succeeds\n   Derived* d2 = dynamic_cast(b2);   \/\/ fails: returns NULL\n   <\/pre>\n\n\n\n<p>\u5982\u679c\u7c7b\u578b\u8f6c\u6362\u88ab\u7528\u5728\u5f15\u7528(reference)\u7c7b\u578b\u4e0a\uff0c\u800c\u8fd9\u4e2a\u8f6c\u6362\u4e0d\u53ef\u80fd\u8fdb\u884c\u7684\u8bdd\uff0c\u4e00\u4e2abad_cast \u7c7b\u578b\u7684\u4f8b\u5916(exception)\u5c06\u4f1a\u88ab\u629b\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  class Base { virtual dummy(){}; };<br>  class Derived : public Base { };<br>  <br>  Base* b1 = new Derived;<br>  Base* b2 = new Base;<br>  Derived d1 = dynamic_cast(b1);   \/\/ succeeds\n  Derived d2 = dynamic_cast(b2);   \/\/ fails: exception thrown\n  <\/pre>\n\n\n\n<h3>const_cast<\/h3>\n\n\n\n<p>\u8fd9\u79cd\u7c7b\u578b\u8f6c\u6362\u5bf9\u5e38\u91cfconst \u8fdb\u884c\u8bbe\u7f6e\u6216\u53d6\u6d88\u64cd\u4f5c\uff1aclass C {};<br>const C * a = new C;<br>C * b = const_cast&lt;C*&gt; (a);<\/p>\n\n\n\n<p>\u5176\u4ed63\u79cdcast \u64cd\u4f5c\u7b26\u90fd\u4e0d\u53ef\u4ee5\u4fee\u6539\u4e00\u4e2a\u5bf9\u8c61\u7684\u5e38\u91cf\u5c5e\u6027(constness)\u3002<\/p>\n\n\n\n<h3>typeid<\/h3>\n\n\n\n<p>ANSI-C++ \u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b0\u7684\u64cd\u4f5c\u7b26\u53eb\u505a typeid \uff0c\u5b83\u68c0\u67e5\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\uff1a<code>typeid (expression)<\/code><\/p>\n\n\n\n<p>\u8fd9\u4e2a\u64cd\u4f5c\u7b26\u8fd4\u56de\u4e00\u4e2a\u7c7b\u578b\u4e3atype_info\u7684\u5e38\u91cf\u5bf9\u8c61\u6307\u9488\uff0c\u8fd9\u79cd\u7c7b\u578b\u5b9a\u4e49\u5728\u6807\u51c6\u5934\u51fd\u6570\u4e2d\u3002\u8fd9\u79cd\u8fd4\u56de\u503c\u53ef\u4ee5\u7528\u64cd\u4f5c\u7b26 == \u548c != \u6765\u4e92\u76f8\u8fdb\u884c\u6bd4\u8f83\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u901a\u8fc7name()\u51fd\u6570\u83b7\u5f97\u4e00\u4e2a\u63cf\u8ff0\u6570\u636e\u7c7b\u578b\u6216\u7c7b\u540d\u79f0\u7684\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ typeid, typeinfo #include &lt;iostream.h&gt; #include &lt;typeinfo&gt; class CDummy { }; int main () { CDummy* a,b; if (typeid(a) != typeid(b)) { cout &lt;&lt; &#8220;a and b are of different types:\\n&#8221;; cout &lt;&lt; &#8220;a is: &#8221; &lt;&lt; typeid(a).name() &lt;&lt; &#8216;\\n&#8217;; cout &lt;&lt; &#8220;b is: &#8221; &lt;&lt; typeid(b).name() &lt;&lt; &#8216;\\n&#8217;; } return 0; }<\/td><td>a and b are of different types:<br>a is: class CDummy *<br>b is: class CDummy<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>5.4 \u7c7b\u578b\u8f6c\u6362\u9ad8\u7ea7 (Advacned Class Type-casting) \u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u4e00\u76f4\u4f7f\u7528\u4f20\u7edf\u7684\u7c7b\u578b\u8f6c\u6362\u7b26\u6765\u8fdb\u884c\u7b80\u5355\u5bf9\u8c61\u7684\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u8981\u628a\u4e00\u4e2adouble\u7c7b\u578b\u7684\u6d6e\u70b9\u578b\u6570\u5b57\u8f6c\u6362\u4e3aint \u7684\u6574\u578b\u6570\u5b57\uff0c\u6211\u4eec\u662f\u8fd9\u6837\u505a\u7684\uff1aint i;double d;i = (int) d; \u6216\u8005i = int (d); \u8fd9\u6837\u505a\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u65f6\u6ca1\u95ee\u9898\u7684\uff0c\u56e0\u4e3a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u5df2\u7ecf\u6709\u6807\u51c6\u7684\u5b9a\u4e49\u3002\u540c\u6837\u7684\u64cd\u4f5c\u4e5f\u53ef\u4ee5\u88ab\u5728\u7c7b\u6216\u7c7b\u7684\u6307\u9488\u4e0a\uff0c\u56e0\u6b64\u4ee5\u4e0b\u4f8b\u5b50\u4e2d\u7684\u5199\u6cd5\u4e5f\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff1a \/\/ class type-casting #include &lt;iostream.h&gt; class CDummy { int i; }; class CAddition { int x,y; public: CAddition (int a, int b) { x=a; y=b; } int result() { return x+y;} }; int main () { CDummy d; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":361,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/371"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=371"}],"version-history":[{"count":1,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/371\/revisions"}],"predecessor-version":[{"id":372,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/371\/revisions\/372"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/361"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}