{"id":4522,"date":"2020-08-21T14:21:01","date_gmt":"2020-08-21T06:21:01","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4522"},"modified":"2020-10-23T18:01:55","modified_gmt":"2020-10-23T10:01:55","slug":"%e8%99%9a%e7%bb%a7%e6%89%bf%e7%9a%84vtable","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4522","title":{"rendered":"C++ \u591a\u7ee7\u627f\u548c\u865a\u7ee7\u627f\u7684\u5185\u5b58\u5e03\u5c40"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><\/div>\n<div class=\"article-info-box\">\n<div class=\"article-bar-top\">\n<div><a href=\"https:\/\/blog.csdn.net\/yockie\/article\/details\/50603236\">\u539f\u6587\u5730\u5740<\/a><\/div>\n<\/div>\n<div class=\"operating\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix\">\n<div id=\"content_views\" class=\"markdown_views\">\n<p>\u8b66\u544a. \u672c\u6587\u6709\u70b9\u6280\u672f\u96be\u5ea6\uff0c\u9700\u8981\u8bfb\u8005\u4e86\u89e3C++\u548c\u4e00\u4e9b\u6c47\u7f16\u8bed\u8a00\u77e5\u8bc6\u3002<br \/>\n\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u89e3\u91ca\u7531gcc\u7f16\u8bd1\u5668\u5b9e\u73b0\u591a\u7ee7\u627f\u548c\u865a\u7ee7\u627f\u7684\u5bf9\u8c61\u7684\u5e03\u5c40\u3002\u867d\u7136\u5728\u7406\u60f3\u7684C++\u7a0b\u5e8f\u4e2d\u4e0d\u9700\u8981\u77e5\u9053\u8fd9\u4e9b\u7f16\u8bd1\u5668\u5185\u90e8\u7ec6\u8282\uff0c\u4f46\u4e0d\u5e78\u7684\u662f\u591a\u91cd\u7ee7\u627f\uff08\u7279\u522b\u662f\u865a\u62df\u7ee7\u627f\uff09\u7684\u5b9e\u73b0\u65b9\u5f0f\u6709\u5404\u79cd\u5404\u6837\u7684\u4e0d\u592a\u660e\u786e\u7684\u7ed3\u8bba\uff08\u5c24\u5176\u662f\uff0c\u5173\u4e8e<a href=\"http:\/\/www.phpcompiler.org\/articles\/virtualinheritance.html#Downcasting\">\u5411\u4e0b\u8f6c\u578b\u6307\u9488<\/a>\uff0c\u4f7f\u7528<a href=\"http:\/\/www.phpcompiler.org\/articles\/virtualinheritance.html#DoublePointers\">\u6307\u5411\u6307\u9488\u7684\u6307\u9488<\/a>\uff0c\u8fd8\u6709<a href=\"http:\/\/www.phpcompiler.org\/articles\/virtualinheritance.html#VirtualConstructors\">\u865a\u62df\u57fa\u7c7b\u7684\u6784\u9020\u65b9\u6cd5<\/a>\u7684\u8c03\u7528\u547d\u4ee4\uff09\u3002 \u5982\u679c\u4f60\u4e86\u89e3\u591a\u91cd\u7ee7\u627f\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u4f60\u5c31\u80fd\u9884\u89c1\u5230\u8fd9\u4e9b\u7ed3\u8bba\u5e76\u8fd0\u7528\u5230\u4f60\u7684\u4ee3\u7801\u4e2d\u3002\u800c\u4e14\uff0c\u5982\u679c\u4f60\u5173\u5fc3\u6027\u80fd\uff0c\u7406\u89e3\u865a\u62df\u7ee7\u627f\u7684\u5f00\u9500\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u6700\u540e\uff0c\u8fd9\u5f88\u6709\u8da3\u3002 \ud83d\ude42<\/p>\n<h1 id=\"\u591a\u91cd\u7ee7\u627f\"><a name=\"t0\"><\/a><a name=\"t0\"><\/a>\u591a\u91cd\u7ee7\u627f<\/h1>\n<p>\u9996\u5148\u6211\u4eec\u8003\u8651\u4e00\u4e2a\uff08\u975e\u865a\u62df\uff09\u591a\u91cd\u7ee7\u627f\u7684\u76f8\u5bf9\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u770b\u770b\u4e0b\u9762\u7684C++\u7c7b\u5c42\u6b21\u7ed3\u6784\u3002<\/p>\n<pre class=\"prettyprint\"><code class=\"language-C++ hljs vbscript has-numbering\"><span class=\"hljs-keyword\">class<\/span> Top\r\n{ \r\n    <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-built_in\">int<\/span> a;\r\n}; \r\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-built_in\">Left<\/span> : <span class=\"hljs-keyword\">public<\/span> Top\r\n{ \r\n    <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-built_in\">int<\/span> b;\r\n}; \r\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-built_in\">Right<\/span> : <span class=\"hljs-keyword\">public<\/span> Top\r\n{ \r\n    <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-built_in\">int<\/span> c;\r\n}; \r\n<span class=\"hljs-keyword\">class<\/span> Bottom : <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-built_in\">Left<\/span>, <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-built_in\">Right<\/span>\r\n{ \r\n    <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-built_in\">int<\/span> d;\r\n};<\/code><\/pre>\n<p>\u4f7f\u7528UML\u56fe\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u4e2a\u5c42\u6b21\u7ed3\u6784\u8868\u793a\u4e3a\uff1a<br \/>\n<img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20160128151123315\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n\u6ce8\u610fTop\u88ab\u7ee7\u627f\u4e86\u4e24\u6b21\uff08\u5728Eiffel\u8bed\u8a00\u4e2d\u8fd9\u88ab\u79f0\u4f5c\u91cd\u590d\u7ee7\u627f\uff09\u3002\u8fd9\u610f\u5473\u7740\u7c7b\u578bBottom\u7684\u4e00\u4e2a\u5b9e\u4f8bbottom\u5c06\u6709\u4e24\u4e2a\u53eb\u505aa\u7684\u5143\u7d20\uff08\u5206\u522b\u4e3abottom.Left::a\u548cbottom.Right::a\uff09\u3002<\/p>\n<p>Left\u3001Right\u548cBottom\u5728\u5185\u5b58\u4e2d\u662f\u5982\u4f55\u5e03\u5c40\u7684\uff1f\u8ba9\u6211\u4eec\u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u3002Left\u548cRight\u62e5\u6709\u5982\u4e0b\u7684\u7ed3\u6784\uff1a<\/p>\n<div class=\"table-box\">\n<table>\n<thead>\n<tr>\n<th><strong>Left<\/strong><\/th>\n<th><strong>Right<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Top::a<\/td>\n<td>Top::a<\/td>\n<\/tr>\n<tr>\n<td>Left::b<\/td>\n<td>Right::c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u8bf7\u6ce8\u610f\u7b2c\u4e00\u4e2a\u5c5e\u6027\u662f\u4eceTop\u7ee7\u627f\u4e0b\u6765\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u4e0b\u9762\u4e24\u6761\u8bed\u53e5\u540e<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs vbscript has-numbering\"><span class=\"hljs-built_in\">Left<\/span>* <span class=\"hljs-built_in\">left<\/span> = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Left<\/span>();\r\nTop* top = <span class=\"hljs-built_in\">left<\/span>;<\/code><\/pre>\n<p>left\u548ctop\u6307\u5411\u4e86\u540c\u4e00\u5730\u5740\uff0c\u6211\u4eec\u53ef\u4ee5\u628aLeft Object\u5f53\u6210Top Object\u6765\u4f7f\u7528(\u5f88\u660e\u663e\uff0cRight\u4e0e\u6b64\u4e5f\u7c7b\u4f3c)\u3002\u90a3Buttom\u5462\uff1fGCC\u7684\u5efa\u8bae\u5982\u4e0b\uff1a<\/p>\n<div class=\"table-box\">\n<table>\n<thead>\n<tr>\n<th><strong>Bottom<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Left::Top::a<\/td>\n<\/tr>\n<tr>\n<td>Left::b<\/td>\n<\/tr>\n<tr>\n<td>Right::Top::a<\/td>\n<\/tr>\n<tr>\n<td>Right::c<\/td>\n<\/tr>\n<tr>\n<td>Bottom::d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u5982\u679c\u6211\u4eec\u63d0\u5347Bottom\u6307\u9488\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u4e8b\u5462\uff1f<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs mathematica has-numbering\"><span class=\"hljs-keyword\">Bottom<\/span>* bottom = new <span class=\"hljs-keyword\">Bottom<\/span>();\r\n<span class=\"hljs-keyword\">Left<\/span>* left = bottom;<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5de5\u4f5c\u6b63\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u628a\u4e00\u4e2aBottom\u7684\u5bf9\u8c61\u5f53\u4f5c\u4e00\u4e2aLeft\u5bf9\u8c61\u6765\u4f7f\u7528\uff0c\u56e0\u4e3a\u4e24\u4e2a\u7c7b\u7684\u5185\u5b58\u90e8\u5c40\u662f\u4e00\u6837\u7684\u3002\u90a3\u4e48\uff0c\u5982\u679c\u5c06\u5176\u63d0\u5347\u4e3aRight\u5462\uff1f\u4f1a\u53d1\u751f\u4ec0\u4e48\u4e8b\uff1f<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs vbscript has-numbering\"><span class=\"hljs-built_in\">Right<\/span>* <span class=\"hljs-built_in\">right<\/span> = bottom;<\/code><\/pre>\n<p>\u4e3a\u4e86\u6267\u884c\u8fd9\u6761\u8bed\u53e5\uff0c\u6211\u4eec\u9700\u8981\u5224\u65ad\u6307\u9488\u7684\u503c\u4ee5\u4fbf\u8ba9\u5b83\u6307\u5411Bottom\u4e2d\u5bf9\u5e94\u7684\u6bb5\u3002<\/p>\n<div class=\"table-box\">\n<table>\n<thead>\n<tr>\n<th><strong>Bottom<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Left::Top::a<\/td>\n<\/tr>\n<tr>\n<td>Left::b<\/td>\n<\/tr>\n<tr>\n<td>Right::Top::a &lt;\u2014rightpoints<\/td>\n<\/tr>\n<tr>\n<td>Right::c<\/td>\n<\/tr>\n<tr>\n<td>Bottom::d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u7ecf\u8fc7\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u64cd\u4f5c\u6b63\u5e38Right\u5bf9\u8c61\u4e00\u6837\u4f7f\u7528right\u6307\u9488\u8bbf\u95eebottom\u3002\u867d\u7136\uff0cbottom\u4e0eright\u73b0\u5728\u6307\u5411\u4e24\u4e2a\u4e0d\u540c\u7684\u5185\u5b58\u5730\u5740\u3002\u51fa\u4e8e\u5b8c\u6574\u6027\u7684\u7f18\u6545\uff0c\u601d\u8003\u4e00\u4e0b\u6267\u884c\u4e0b\u9762\u8fd9\u6761\u8bed\u53e5\u65f6\u4f1a\u51fa\u73b0\u4ec0\u4e48\u72b6\u51b5\u3002<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs fix has-numbering\"><span class=\"hljs-attribute\">Top* top <\/span>=<span class=\"hljs-string\"> bottom;<\/span><\/code><\/pre>\n<p>\u662f\u7684\uff0c\u4ec0\u4e48\u4e5f\u6ca1\u6709\u3002\u8fd9\u6761\u8bed\u53e5\u662f\u6709\u6b67\u4e49\u7684\uff1a\u7f16\u8bd1\u5668\u5c06\u4f1a\u62a5\u9519\u3002<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs asciidoc has-numbering\">error: <span class=\"hljs-smartquote\">`Top'<\/span> is an ambiguous base of <span class=\"hljs-smartquote\">`Bottom'<\/span><\/code><\/pre>\n<p>\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u907f\u514d\u8fd9\u6837\u7684\u6b67\u4e49<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs mathematica has-numbering\"><span class=\"hljs-keyword\">Top<\/span>* topL = (<span class=\"hljs-keyword\">Left<\/span>*) bottom;\r\n<span class=\"hljs-keyword\">Top<\/span>* topR = (<span class=\"hljs-keyword\">Right<\/span>*) bottom;<\/code><\/pre>\n<p>\u6267\u884c\u8fd9\u4e24\u6761\u8bed\u53e5\u540e\uff0ctopL\u548cleft\u4f1a\u6307\u5411\u540c\u6837\u7684\u5730\u5740\uff0ctopR\u548cright\u4e5f\u4f1a\u6307\u5411\u540c\u6837\u7684\u5730\u5740\u3002<\/p>\n<h1 id=\"\u865a\u62df\u7ee7\u627f\"><a name=\"t1\"><\/a><a name=\"t1\"><\/a>\u865a\u62df\u7ee7\u627f<\/h1>\n<p>\u4e3a\u4e86\u907f\u514d\u91cd\u590d\u7ee7\u627fTop\uff0c\u6211\u4eec\u5fc5\u987b\u865a\u62df\u7ee7\u627fTop\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs cs has-numbering\">class Top\r\n{ \r\n    <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-keyword\">int<\/span> a;\r\n}; \r\nclass Left : <span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-keyword\">public<\/span> Top\r\n{ \r\n    <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-keyword\">int<\/span> b;\r\n}; \r\nclass Right : <span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-keyword\">public<\/span> Top\r\n{ \r\n    <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-keyword\">int<\/span> c;\r\n}; \r\nclass Bottom : <span class=\"hljs-keyword\">public<\/span> Left, <span class=\"hljs-keyword\">public<\/span> Right\r\n{ \r\n    <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-keyword\">int<\/span> d;\r\n};<\/code><\/pre>\n<p>\u8fd9\u5c31\u5f97\u5230\u4e86\u5982\u4e0b\u7684\u5c42\u6b21\u7ed3\u6784\uff08\u4e5f\u8bb8\u662f\u4f60\u4e00\u5f00\u59cb\u5c31\u60f3\u5f97\u5230\u7684\uff09<br \/>\n<img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20160128201653149\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n\u867d\u7136\u4ece\u7a0b\u5e8f\u5458\u7684\u89d2\u5ea6\u770b\uff0c\u8fd9\u4e5f\u8bb8\u66f4\u52a0\u7684\u660e\u663e\u548c\u7b80\u4fbf\uff0c\u4f46\u4ece\u7f16\u8bd1\u5668\u7684\u89d2\u5ea6\u770b\uff0c\u8fd9\u5c31\u53d8\u5f97\u975e\u5e38\u7684\u590d\u6742\u3002\u91cd\u65b0\u8003\u8651\u4e0bBottom\u7684\u5e03\u5c40\uff0c\u5176\u4e2d\u7684\u4e00\u4e2a\uff08\u4e5f\u8bb8\u6ca1\u6709\uff09\u53ef\u80fd\u662f\uff1a<\/p>\n<div class=\"table-box\">\n<table>\n<thead>\n<tr>\n<th><strong>Bottom<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Left::Top::a<\/td>\n<\/tr>\n<tr>\n<td>Left::b<\/td>\n<\/tr>\n<tr>\n<td>Right::c<\/td>\n<\/tr>\n<tr>\n<td>Bottom::d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u8fd9\u4e2a\u5e03\u5c40\u7684\u4f18\u70b9\u662f\uff0c\u5e03\u5c40\u7684\u7b2c\u4e00\u90e8\u5206\u4e0eLeft\u7684\u5e03\u5c40\u91cd\u53e0\u4e86\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u901a\u8fc7\u4e00\u4e2aLeft\u6307\u9488\u8bbf\u95ee Bottom\u7c7b\u3002\u53ef\u662f\u6211\u4eec\u600e\u4e48\u5904\u7406<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs vbscript has-numbering\"><span class=\"hljs-built_in\">Right<\/span>* <span class=\"hljs-built_in\">right<\/span> = bottom;<\/code><\/pre>\n<p>\u6211\u4eec\u5c06\u54ea\u4e2a\u5730\u5740\u8d4b\u7ed9right\u5462? \u7ecf\u8fc7\u8fd9\u4e2a\u8d4b\u503c\uff0c\u5982\u679cright\u662f\u6307\u5411\u4e00\u4e2a\u666e\u901a\u7684Right\u5bf9\u8c61\uff0c\u6211\u4eec\u5e94\u8be5\u5c31\u80fd\u4f7f\u7528 right\u4e86\u3002\u4f46\u662f\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\uff01Right\u672c\u8eab\u7684\u5185\u5b58\u5e03\u5c40\u662f\u5b8c\u5168\u4e0d\u540c\u7684\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u65e0\u6cd5\u50cf\u8bbf\u95ee\u4e00\u4e2a\u201d\u771f\u6b63\u7684\u201dRight\u5bf9\u8c61\u4e00\u6837\uff0c\u6765\u8bbf\u95ee\u5347\u7ea7\u7684Bottom\u5bf9\u8c61\u3002\u800c\u4e14\uff0c\u4e5f\u6ca1\u6709\u5176\u5b83\uff08\u7b80\u5355\u7684\uff09\u53ef\u4ee5\u6b63\u5e38\u8fd0\u4f5c\u7684Bottom\u5e03\u5c40\u3002<br \/>\n\u89e3\u51b3\u529e\u6cd5\u662f\u590d\u6742\u7684\u3002\u6211\u4eec\u5148\u7ed9\u51fa\u89e3\u51b3\u65b9\u6848\uff0c\u4e4b\u540e\u518d\u6765\u89e3\u91ca\u5b83\u3002<\/p>\n<p><img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20160128201848245\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u4f60\u5e94\u8be5\u6ce8\u610f\u5230\u4e86\u8fd9\u4e2a\u56fe\u4e2d\u7684\u4e24\u4e2a\u5730\u65b9\u3002\u7b2c\u4e00\uff0c\u5b57\u6bb5\u7684\u987a\u5e8f\u662f\u5b8c\u5168\u4e0d\u540c\u7684\uff08\u4e8b\u5b9e\u4e0a\uff0c\u5dee\u4e0d\u591a\u662f\u76f8\u53cd\u7684\uff09\u3002\u7b2c\u4e8c\uff0c\u6709\u51e0\u4e2avptr\u6307\u9488\u3002\u8fd9\u4e9b\u5c5e\u6027\u662f\u7531\u7f16\u8bd1\u5668\u6839\u636e\u9700\u8981\u81ea\u52a8\u63d2\u5165\u7684\uff08\u4f7f\u7528\u865a\u62df\u7ee7\u627f\uff0c\u6216\u8005\u4f7f\u7528\u865a\u62df\u51fd\u6570\u7684\u65f6\u5019\uff09\u3002\u7f16\u8bd1\u5668\u4e5f\u5728\u6784\u9020\u5668\u4e2d\u63d2\u5165\u4e86\u4ee3\u7801\uff0c\u6765\u521d\u59cb\u5316\u8fd9\u4e9b\u6307\u9488\u3002<\/p>\n<p>vptr (virtual pointers)\u6307\u5411\u4e00\u4e2a \u201c\u865a\u62df\u8868\u201d\u3002\u7c7b\u7684\u6bcf\u4e2a\u865a\u62df\u57fa\u7c7b\u90fd\u6709\u4e00\u4e2avptr\u6307\u9488\u3002\u8981\u60f3\u77e5\u9053\u8fd9\u4e2a\u865a\u62df\u8868 (vtable)\u662f\u600e\u6837\u8fd0\u7528\u7684\uff0c\u770b\u770b\u4e0b\u9762\u7684C++ \u4ee3\u7801\u3002<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs vbscript has-numbering\">Bottom* bottom = <span class=\"hljs-keyword\">new<\/span> Bottom();\r\n<span class=\"hljs-built_in\">Left<\/span>* <span class=\"hljs-built_in\">left<\/span> = bottom; \r\n<span class=\"hljs-built_in\">int<\/span> p = <span class=\"hljs-built_in\">left<\/span>-&gt;a;<\/code><\/pre>\n<p>\u7b2c\u4e8c\u4e2a\u8d4b\u503c\u4f7fleft\u6307\u5411\u4e86bottom\u7684\u6240\u5728\u5730\u5740\uff08\u5373\uff0c\u5b83\u6307\u5411\u4e86Bottom\u5bf9\u8c61\u7684\u201c\u9876\u90e8\u201d\uff09\u3002\u6211\u4eec\u60f3\u60f3\u6700\u540e\u4e00\u6761\u8d4b\u503c\u8bed\u53e5\u7684\u7f16\u8bd1\u60c5\u51b5\uff08\u7a0d\u5fae\u7b80\u5316\u4e86\uff09\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs perl has-numbering\">movl left, <span class=\"hljs-variable\">%eax<\/span> <span class=\"hljs-comment\"># %eax = left<\/span>\r\nmovl (<span class=\"hljs-variable\">%eax<\/span>), <span class=\"hljs-variable\">%eax<\/span> <span class=\"hljs-comment\"># %eax = left.vptr.Left<\/span>\r\nmovl (<span class=\"hljs-variable\">%eax<\/span>), <span class=\"hljs-variable\">%eax<\/span> <span class=\"hljs-comment\"># %eax = virtual base offset<\/span>\r\naddl left, <span class=\"hljs-variable\">%eax<\/span> <span class=\"hljs-comment\"># %eax = left + virtual base offset<\/span>\r\nmovl (<span class=\"hljs-variable\">%eax<\/span>), <span class=\"hljs-variable\">%eax<\/span> <span class=\"hljs-comment\"># %eax = left.a<\/span>\r\nmovl <span class=\"hljs-variable\">%eax<\/span>, p <span class=\"hljs-comment\"># p = left.a<\/span><\/code><\/pre>\n<p>\u7528\u8bed\u8a00\u6765\u63cf\u8ff0\u7684\u8bdd\uff0c\u5c31\u662f\u6211\u4eec\u7528left\u6307\u5411\u865a\u62df\u8868\uff0c\u5e76\u4e14\u7531\u5b83\u83b7\u5f97\u4e86\u201c\u865a\u62df\u57fa\u7c7b\u504f\u79fb\u201d(vbase)\u3002\u8fd9\u4e2a\u504f\u79fb\u4e4b\u540e\u5c31\u52a0\u5230\u4e86left\uff0c\u7136\u540eleft\u5c31\u7528\u6765\u6307\u5411Bottom\u5bf9\u8c61\u7684Top\u90e8\u5206\u3002\u4ece\u8fd9\u5f20\u56fe\u4f60\u53ef\u4ee5\u770b\u5230Left\u7684\u865a\u62df\u57fa\u7c7b\u504f\u79fb\u662f20\uff1b\u5982\u679c\u5047\u8bbeBottom\u4e2d\u7684\u6240\u6709\u5b57\u6bb5\u90fd\u662f4\u4e2a\u5b57\u8282\uff0c\u90a3\u4e48\u7ed9left\u52a0\u4e0a20\u5b57\u8282\u5c06\u4f1a\u786e\u5b9e\u6307\u5411a\u5b57\u6bb5\u3002<\/p>\n<p>\u7ecf\u8fc7\u8fd9\u4e2a\u8bbe\u7f6e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u540c\u6837\u7684\u65b9\u6cd5\u8bbf\u95eeRight\u90e8\u5206\u3002\u6309\u8fd9\u6837<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs vbscript has-numbering\">Bottom* bottom = <span class=\"hljs-keyword\">new<\/span> Bottom();\r\n<span class=\"hljs-built_in\">Right<\/span>* <span class=\"hljs-built_in\">right<\/span> = bottom; \r\n<span class=\"hljs-built_in\">int<\/span> p = <span class=\"hljs-built_in\">right<\/span>-&gt;a;<\/code><\/pre>\n<p>\u4e4b\u540eright\u5c06\u6307\u5411Bottom\u5bf9\u8c61\u7684\u5408\u9002\u7684\u90e8\u4f4d\uff1a<\/p>\n<div class=\"table-box\">\n<table>\n<thead>\n<tr>\n<th>Bottom<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>vptr.Left<\/td>\n<\/tr>\n<tr>\n<td>Left::b<\/td>\n<\/tr>\n<tr>\n<td>vptr.Right &lt;\u2014right<\/td>\n<\/tr>\n<tr>\n<td>Right::c<\/td>\n<\/tr>\n<tr>\n<td>Bottom::d<\/td>\n<\/tr>\n<tr>\n<td>Top::a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u5bf9top\u7684\u8d4b\u503c\u73b0\u5728\u53ef\u4ee5\u7f16\u8bd1\u6210\u50cf\u524d\u9762Left\u540c\u6837\u7684\u65b9\u5f0f\u3002\u552f\u4e00\u7684\u4e0d\u540c\u5c31\u662f\u73b0\u5728\u7684vptr\u662f\u6307\u5411\u4e86\u865a\u62df\u8868\u7684\u4e0d\u540c\u90e8\u4f4d\uff1a\u53d6\u5f97\u7684\u865a\u62df\u8868\u504f\u79fb\u662f12\uff0c\u8fd9\u5b8c\u5168\u6b63\u786e\uff08\u786e\u5b9a\uff01\uff09\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u56fe\u793a\u6982\u62ec\uff1a<\/p>\n<p><img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20160128202152609\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u5f53\u7136\uff0c\u8fd9\u4e2a\u4f8b\u5b50\u7684\u76ee\u7684\u5c31\u662f\u8981\u50cf\u8bbf\u95ee\u771f\u6b63Right\u5bf9\u8c61\u4e00\u6837\u8bbf\u95ee\u5347\u7ea7\u7684Bottom\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u4e5f\u8981\u7ed9Right\uff08\u548cLeft\uff09\u5e03\u5c40\u5f15\u5165vptrs\uff1a<\/p>\n<p><img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20160128202235704\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u73b0\u5728\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2aRight\u6307\u9488\uff0c\u4e00\u70b9\u4e5f\u4e0d\u8d39\u4e8b\u7684\u8bbf\u95eeBottom\u5bf9\u8c61\u4e86\u3002\u4e0d\u8fc7\uff0c\u8fd9\u662f\u4ed8\u51fa\u4e86\u76f8\u5f53\u5927\u7684\u4ee3\u4ef7\uff1a\u6211\u4eec\u8981\u5f15\u5165\u865a\u62df\u8868\uff0c\u7c7b\u9700\u8981\u6269\u5c55\u4e00\u4e2a\u6216\u66f4\u591a\u4e2a\u865a\u62df\u6307\u9488\uff0c\u5bf9\u4e00\u4e2a\u5bf9\u8c61\u7684\u4e00\u4e2a\u7b80\u5355\u5c5e\u6027\u7684\u67e5\u8be2\u73b0\u5728\u9700\u8981\u4e24\u6b21\u95f4\u63a5\u7684\u901a\u8fc7\u865a\u62df\u8868\uff08\u5373\u4f7f\u7f16\u8bd1\u5668\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u53ef\u4ee5\u51cf\u5c0f\u8fd9\u4e2a\u4ee3\u4ef7\uff09\u3002<\/p>\n<h1 id=\"\u5411\u4e0b\u8f6c\u6362\"><a name=\"t2\"><\/a><a name=\"t2\"><\/a>\u5411\u4e0b\u8f6c\u6362<\/h1>\n<p>\u5982\u6211\u4eec\u6240\u89c1\uff0c\u5c06\u4e00\u4e2a\u6d3e\u751f\u7c7b\u7684\u6307\u9488\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7236\u7c7b\u7684\u6307\u9488(\u6216\u8005\u8bf4\uff0c\u5411\u4e0a\u8f6c\u6362)\u53ef\u80fd\u6d89\u53ca\u5230\u7ed9\u6307\u9488\u589e\u6dfb\u4e00\u4e2a\u504f\u79fb\u3002\u6709\u4eba\u53ef\u80fd\u4f1a\u60f3\u4e86\uff0c\u8fd9\u6837\u5411\u4e0b\u8f6c\u6362\uff08\u53cd\u65b9\u5411\u7684\uff09\u5c31\u53ef\u4ee5\u7b80\u5355\u7684\u901a\u8fc7\u51cf\u53bb\u540c\u6837\u7684\u504f\u79fb\u6765\u5b9e\u73b0\u3002\u786e\u5b9e\uff0c\u5bf9\u975e\u865a\u62df\u7ee7\u627f\u6765\u8bf4\u662f\u8fd9\u6837\u7684\u3002\u53ef\u662f\uff0c\u865a\u62df\u7ee7\u627f\uff08\u6beb\u4e0d\u5947\u602a\u7684\uff01\uff09\u5e26\u6765\u4e86\u53e6\u4e00\u79cd\u590d\u6742\u6027\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u50cf\u4e0b\u9762\u8fd9\u4e2a\u7c7b\u8fd9\u6837\u6269\u5c55\u7ee7\u627f\u5c42\u6b21\u3002<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs vbscript has-numbering\"><span class=\"hljs-keyword\">class<\/span> AnotherBottom : <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-built_in\">Left<\/span>, <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-built_in\">Right<\/span>\r\n{ \r\n    <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-built_in\">int<\/span> e; <span class=\"hljs-built_in\">int<\/span> f;\r\n};<\/code><\/pre>\n<p>\u7ee7\u627f\u5c42\u6b21\u73b0\u5728\u770b\u8d77\u6765\u662f\u8fd9\u6837<\/p>\n<p><img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20160128202337096\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u73b0\u5728\u8003\u8651\u4e00\u4e0b\u4e0b\u9762\u7684\u4ee3\u7801\u3002<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs cpp has-numbering\">Bottom* bottom1 = <span class=\"hljs-keyword\">new<\/span> Bottom();\r\nAnotherBottom* bottom2 = <span class=\"hljs-keyword\">new<\/span> AnotherBottom();\r\nTop* top1 = bottom1;\r\nTop* top2 = bottom2;\r\nLeft* left = <span class=\"hljs-keyword\">static_cast<\/span>&lt;Left*&gt;(top1);<\/code><\/pre>\n<p>\u4e0b\u56fe\u663e\u793a\u4e86Bottom\u548cAnotherBottom\u7684\u5e03\u5c40\uff0c\u800c\u4e14\u5728\u6700\u540e\u4e00\u4e2a\u8d4b\u503c\u540e\u9762\u663e\u793a\u4e86\u6307\u5411top\u7684\u6307\u9488\u3002<\/p>\n<p><img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20160128202445770\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u73b0\u5728\u8003\u8651\u4e00\u4e0b\u600e\u4e48\u53bb\u5b9e\u73b0\u4ecetop1\u5230left\u7684\u9759\u6001\u8f6c\u6362\uff0c\u540c\u65f6\u8981\u60f3\u5230\uff0c\u6211\u4eec\u5e76\u4e0d\u77e5\u9053top1\u662f\u5426\u6307\u5411\u4e00\u4e2aBottom\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u6216\u8005\u662f\u6307\u5411\u4e00\u4e2aAnotherBottom\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u6240\u4ee5\u8fd9\u529e\u4e0d\u5230\uff01\u8fd9\u4e2a\u91cd\u8981\u7684\u504f\u79fb\u4f9d\u8d56\u4e8etop1\u8fd0\u884c\u65f6\u7684\u7c7b\u578b\uff08Bottom\u521920\uff0cAnotherBottom\u521924\uff09\u3002\u7f16\u8bd1\u5668\u5c06\u62a5\u9519\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs asciidoc has-numbering\">error: cannot convert from base <span class=\"hljs-smartquote\">`Top'<\/span> to derived type <span class=\"hljs-smartquote\">`Left'<\/span>\r\nvia virtual base <span class=\"hljs-smartquote\">`Top'<\/span><\/code><\/pre>\n<p>\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u8fd0\u884c\u65f6\u7684\u4fe1\u606f\uff0c\u6240\u4ee5\u5e94\u8be5\u7528\u4e00\u4e2a\u52a8\u6001\u8f6c\u6362\u6765\u66ff\u4ee3\u5b9e\u73b0\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs cpp has-numbering\">Left* left = <span class=\"hljs-keyword\">dynamic_cast<\/span>&lt;Left*&gt;(top1);<\/code><\/pre>\n<p>\u53ef\u662f\uff0c\u7f16\u8bd1\u5668\u4ecd\u7136\u4e0d\u6ee1\u610f\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs haskell has-numbering\"><span class=\"hljs-title\">error<\/span>: cannot dynamic_cast `top' (<span class=\"hljs-keyword\">of<\/span> <span class=\"hljs-typedef\"><span class=\"hljs-keyword\">type<\/span> `class <span class=\"hljs-type\">Top<\/span>*') to <span class=\"hljs-keyword\">type<\/span><\/span>\r\n   `<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-type\">Left<\/span>*' (source <span class=\"hljs-typedef\"><span class=\"hljs-keyword\">type<\/span> is not polymorphic)<\/span><\/code><\/pre>\n<p>\uff08\u6ce8\uff1apolymorphic\u591a\u6001\u7684\uff09<\/p>\n<p>\u95ee\u9898\u5728\u4e8e\uff0c\u52a8\u6001\u8f6c\u6362\uff08\u8f6c\u6362\u4e2d\u4f7f\u7528\u5230typeid\uff09\u9700\u8981top1\u6240\u6307\u5411\u5bf9\u8c61\u7684\u8fd0\u884c\u65f6\u7c7b\u578b\u4fe1\u606f\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u770b\u770b\u8fd9\u5f20\u56fe\uff0c\u4f60\u5c31\u4f1a\u53d1\u73b0\uff0c\u5728top1\u6307\u5411\u7684\u4f4d\u7f6e\uff0c\u6211\u4eec\u4ec5\u4ec5\u53ea\u6709\u4e00\u4e2ainteger (a)\u800c\u5df2\u3002\u7f16\u8bd1\u5668\u6ca1\u6709\u5305\u542b\u6307\u5411Top\u7684\u865a\u62df\u6307\u9488\uff0c\u56e0\u4e3a\u5b83\u4e0d\u8ba4\u4e3a\u8fd9\u662f\u5fc5\u9700\u7684\u3002\u4e3a\u4e86\u5f3a\u5236\u7f16\u8bd1\u5668\u5305\u542b\u8fdb\u8fd9\u4e2avptr\u6307\u9488\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed9Top\u589e\u52a0\u4e00\u4e2a\u865a\u62df\u7684\u6790\u6784\u5668\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs cs has-numbering\">class Top\r\n{ \r\n<span class=\"hljs-keyword\">public<\/span>: \r\n    <span class=\"hljs-keyword\">virtual<\/span> ~<span class=\"hljs-title\">Top<\/span>() {}\r\n    <span class=\"hljs-keyword\">int<\/span> a;\r\n};<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4fee\u6539\u9700\u8981\u6307\u5411Top\u7684vptr\u6307\u9488\u3002Bottom\u7684\u65b0\u5e03\u5c40\u662f<\/p>\n<p><img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20160128202702630\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>(\u5f53\u7136\u7c7b\u4f3c\u7684\u5176\u5b83\u7c7b\u4e5f\u6709\u4e00\u4e2a\u65b0\u7684\u6307\u5411Top\u7684vptr\u6307\u9488)\u3002\u73b0\u5728\u7f16\u8bd1\u5668\u4e3a\u52a8\u6001\u8f6c\u6362\u63d2\u8fdb\u4e86\u4e00\u4e2a\u5e93\u8c03\u7528\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs bash has-numbering\">left = __dynamic_cast(top1, typeinfo_<span class=\"hljs-keyword\">for<\/span>_Top, typeinfo_<span class=\"hljs-keyword\">for<\/span>_Left, -<span class=\"hljs-number\">1<\/span>);<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570__dynamic_cast\u5b9a\u4e49\u5728stdc++\u5e93\u4e2d(\u76f8\u5e94\u7684\u5934\u6587\u4ef6\u662fcxxabi.h)\uff1b\u53c2\u6570\u4e3aTop\u7684\u7c7b\u578b\u4fe1\u606f\uff0cLeft\u548cBottom(\u901a\u8fc7vptr.Top)\uff0c\u8fd9\u4e2a\u8f6c\u6362\u53ef\u4ee5\u6267\u884c\u3002 (\u53c2\u6570 -1 \u6807\u793a\u51faLeft\u548cTop\u4e4b\u95f4\u7684\u5173\u7cfb\u73b0\u5728\u8fd8\u662f\u672a\u77e5)\u3002\u66f4\u591a\u8be6\u7ec6\u8d44\u6599\uff0c\u8bf7\u53c2\u8003tinfo.cc \u7684\u5177\u4f53\u5b9e\u73b0 \u3002<\/p>\n<h1 id=\"\u603b\u7ed3\u8bed\"><a name=\"t3\"><\/a><a name=\"t3\"><\/a>\u603b\u7ed3\u8bed<\/h1>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e9b\u6ca1\u4e86\u7ed3\u7684\u90e8\u5206\u3002<\/p>\n<h2 id=\"\u6307\u9488\u7684\u6307\u9488\"><a name=\"t4\"><\/a><a name=\"t4\"><\/a>\u6307\u9488\u7684\u6307\u9488<\/h2>\n<p>\u8fd9\u91cc\u51fa\u73b0\u4e86\u4e00\u70b9\u4ee4\u4eba\u8ff7\u60d1\u7684\u95ee\u9898\uff0c\u4f46\u662f\u5982\u679c\u4f60\u4ed4\u7ec6\u601d\u8003\u4e0b\u4e00\u7684\u8bdd\u5b83\u5176\u5b9e\u5f88\u7b80\u5355\u3002\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\u3002\u5047\u8bbe\u4f7f\u7528\u4e0a\u4e00\u8282\u7528\u5230\u7684\u7c7b\u5c42\u6b21\u7ed3\u6784(\u5411\u4e0b\u7c7b\u578b\u8f6c\u6362).\u5728\u524d\u9762\u7684\u5c0f\u8282\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u5b83\u7684\u7ed3\u679c\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs mathematica has-numbering\"><span class=\"hljs-keyword\">Bottom<\/span>* b = new <span class=\"hljs-keyword\">Bottom<\/span>();\r\n<span class=\"hljs-keyword\">Right<\/span>* r = b;<\/code><\/pre>\n<p>(\u5728\u5c06b\u7684\u503c\u8d4b\u7ed9r\u4e4b\u524d\uff0c\u9700\u8981\u5c06\u5b83\u8c03\u65748\u4e2a\u5b57\u8282\uff0c\u4ece\u800c\u8ba9\u5b83\u6307\u5411Bottom\u5bf9\u8c61\u7684Right\u90e8\u5206).\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5408\u6cd5\u5730\u5c06\u4e00\u4e2aBottom* \u8d4b\u503c\u7ed9\u4e00\u4e2aRight*\u7684\u6307\u9488\u3002\u4f46\u662fBottom**\u548cRight**\u53c8\u4f1a\u600e\u6837\u5462\uff1f<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs mathematica has-numbering\"><span class=\"hljs-keyword\">Bottom<\/span>** bb = &amp;b;\r\n<span class=\"hljs-keyword\">Right<\/span>** rr = bb;<\/code><\/pre>\n<p>\u7f16\u8bd1\u5668\u4f1a\u63a5\u53d7\u8fd9\u6837\u7684\u5f62\u5f0f\u5417\uff1f\u6211\u4eec\u5feb\u901f\u6d4b\u8bd5\u4e00\u4e0b\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs asciidoc has-numbering\">error: invalid conversion from <span class=\"hljs-smartquote\">`Bottom**'<\/span> to <span class=\"hljs-smartquote\">`Right**'<\/span><\/code><\/pre>\n<p>\u4e3a\u4ec0\u4e48\u5462\uff1f\u5047\u8bbe\u7f16\u8bd1\u5668\u53ef\u4ee5\u63a5\u53d7\u4ecebb\u5230rr\u7684\u8d4b\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u53ea\u7ba1\u7684\u770b\u5230\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20160128202859615\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u56e0\u6b64\uff0cbb\u548crr\u90fd\u6307\u5411b\uff0c\u5e76\u4e14b\u548cr\u6307\u5411Bottom\u5bf9\u8c61\u7684\u6b63\u786e\u7684\u7ae0\u8282\u3002\u73b0\u5728\u8003\u8651\u5f53\u6211\u4eec\u8d4b\u503c\u7ed9<em>rr\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48(\u6ce8\u610f*rr\u7684\u7c7b\u578b\u65f6Right<\/em>,\u56e0\u6b64\u8fd9\u4e2a\u8d4b\u503c\u662f\u6709\u6548\u7684):<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs fix has-numbering\"><span class=\"hljs-attribute\">*rr <\/span>=<span class=\"hljs-string\"> b;   <\/span><\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u8d4b\u503c\u548c\u4e0a\u9762\u7684\u8d4b\u503c\u7ed9r\u5728\u6839\u672c\u4e0a\u662f\u4e00\u81f4\u7684\u3002\u56e0\u6b64\uff0c\u7f16\u8bd1\u5668\u4f1a\u7528\u540c\u6837\u7684\u65b9\u5f0f\u5b9e\u73b0\u5b83\uff01\u7279\u522b\u5730\uff0c\u5b83\u4f1a\u5728\u8d4b\u503c\u7ed9*rr\u4e4b\u524d\u5c06b\u7684\u503c\u8c03\u65748\u4e2a\u5b57\u8282\u3002\u529e\u4e8b*rr\u6307\u5411\u7684\u662fb\uff01\u6211\u4eec\u518d\u4e00\u6b21\u56fe\u793a\u5316\u8fd9\u4e2a\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" title=\"\" src=\"https:\/\/img-blog.csdn.net\/20160128202946746\" alt=\"\u8fd9\u91cc\u5199\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u53ea\u8981\u6211\u4eec\u901a\u8fc7*rr\u6765\u8bbf\u95eeBottom\u5bf9\u8c61\u8fd9\u90fd\u662f\u6b63\u786e\u7684\uff0c\u4f46\u662f\u53ea\u8981\u6211\u4eec\u901a\u8fc7b\u81ea\u8eab\u6765\u8bbf\u95ee\u5b83\uff0c\u6240\u6709\u7684\u5185\u5b58\u5f15\u7528\u90fd\u4f1a\u67098\u4e2a\u5b57\u8282\u7684\u504f\u79fb\u2014\u660e\u663e\u8fd9\u662f\u4e2a\u4e0d\u7406\u60f3\u7684\u60c5\u51b5\u3002<br \/>\n\u56e0\u6b64\uff0c\u603b\u7684\u6765\u8bf4\uff0c\u53ca\u65f6*a \u548c*b\u901a\u8fc7\u4e00\u4e9b\u5b50\u7c7b\u578b\u76f8\u5173\uff0c**aa\u548c**bb\u5374\u662f\u4e0d\u76f8\u5173\u7684\u3002<\/p>\n<h2 id=\"\u865a\u62df\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570\"><a name=\"t5\"><\/a><a name=\"t5\"><\/a>\u865a\u62df\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570<\/h2>\n<p>\u7f16\u8bd1\u5668\u5fc5\u987b\u786e\u4fdd\u5bf9\u8c61\u7684\u6240\u6709\u865a\u6307\u9488\u90fd\u88ab\u6b63\u786e\u7684\u521d\u59cb\u5316\u3002\u7279\u522b\u662f\uff0c\u7f16\u8bd1\u5668\u786e\u4fdd\u4e86\u7c7b\u7684\u6240\u6709\u865a\u57fa\u7c7b\u90fd\u88ab\u8c03\u7528\uff0c\u5e76\u4e14\u53ea\u88ab\u8c03\u7528\u4e00\u6b21\u3002\u5982\u679c\u4f60\u4e0d\u663e\u793a\u5730\u8c03\u7528\u865a\u62df\u8d85\u7c7b(\u4e0d\u7ba1\u4ed6\u4eec\u5728\u7ee7\u627f\u5c42\u6b21\u7ed3\u6784\u4e2d\u7684\u8ddd\u79bb\u6709\u591a\u8fdc)\uff0c\u7f16\u8bd1\u5668\u90fd\u4f1a\u81ea\u52a8\u5730\u63d2\u5165\u8c03\u7528\u4ed6\u4eec\u7f3a\u7701\u6784\u9020\u51fd\u6570\u3002<br \/>\n\u8fd9\u6837\u4e5f\u4f1a\u5f15\u6765\u4e00\u4e9b\u4e0d\u53ef\u4ee5\u9884\u671f\u7684\u9519\u8bef\u3002\u4ee5\u4e0a\u9762\u7ed9\u51fa\u7684\u7c7b\u5c42\u6b21\u7ed3\u6784\u4f5c\u4e3a\u793a\u4f8b\uff0c\u5e76\u6dfb\u52a0\u4e0a\u6784\u9020\u51fd\u6570\u7684\u90e8\u5206\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs vbscript has-numbering\"><span class=\"hljs-keyword\">class<\/span> Top\r\n{ \r\n<span class=\"hljs-keyword\">public<\/span>:\r\n    Top() \r\n    { \r\n        printf(<span class=\"hljs-string\">\"Top()\\n\"<\/span>);\r\n        a = -<span class=\"hljs-number\">1<\/span>; \r\n    }\r\n    Top(<span class=\"hljs-built_in\">int<\/span> _a) \r\n    { \r\n        printf(<span class=\"hljs-string\">\"Top(int _a=%d)\\n\"<\/span>, _a);\r\n        a = _a; \r\n    } \r\n    <span class=\"hljs-built_in\">int<\/span> a;\r\n}; \r\n\r\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-built_in\">Left<\/span> : virtual <span class=\"hljs-keyword\">public<\/span> Top\r\n{ \r\n<span class=\"hljs-keyword\">public<\/span>:\r\n    <span class=\"hljs-built_in\">Left<\/span>() \r\n    {\r\n        printf(<span class=\"hljs-string\">\"Left()\\n\"<\/span>);\r\n        b = -<span class=\"hljs-number\">2<\/span>; \r\n    }\r\n    <span class=\"hljs-built_in\">Left<\/span>(<span class=\"hljs-built_in\">int<\/span> _a, <span class=\"hljs-built_in\">int<\/span> _b) : Top(_a) \r\n    { \r\n        printf(<span class=\"hljs-string\">\"Left(int _a=%d, int _b=%d)\\n\"<\/span>, _a, _b);\r\n        b = _b; \r\n    } \r\n    <span class=\"hljs-built_in\">int<\/span> b;\r\n}; \r\n\r\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-built_in\">Right<\/span> : virtual <span class=\"hljs-keyword\">public<\/span> Top\r\n{ \r\n<span class=\"hljs-keyword\">public<\/span>:\r\n    <span class=\"hljs-built_in\">Right<\/span>() \r\n    { \r\n        printf(<span class=\"hljs-string\">\"Right()\\n\"<\/span>);\r\n        c = -<span class=\"hljs-number\">3<\/span>; \r\n    }\r\n    <span class=\"hljs-built_in\">Right<\/span>(<span class=\"hljs-built_in\">int<\/span> _a, <span class=\"hljs-built_in\">int<\/span> _c) : Top(_a) \r\n    { \r\n        printf(<span class=\"hljs-string\">\"Right(int _a=%d, int _c=%d)\\n\"<\/span>, _a, _c);\r\n        c = _c; \r\n    } \r\n    <span class=\"hljs-built_in\">int<\/span> c;\r\n}; \r\n\r\n<span class=\"hljs-keyword\">class<\/span> Bottom : <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-built_in\">Left<\/span>, <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-built_in\">Right<\/span>\r\n{ \r\n<span class=\"hljs-keyword\">public<\/span>:\r\n    Bottom() \r\n    { \r\n        printf(<span class=\"hljs-string\">\"Bottom()\\n\"<\/span>);\r\n        d = -<span class=\"hljs-number\">4<\/span>; \r\n    }\r\n    Bottom(<span class=\"hljs-built_in\">int<\/span> _a, <span class=\"hljs-built_in\">int<\/span> _b, <span class=\"hljs-built_in\">int<\/span> _c, <span class=\"hljs-built_in\">int<\/span> _d) : <span class=\"hljs-built_in\">Left<\/span>(_a, _b), <span class=\"hljs-built_in\">Right<\/span>(_a, _c)\r\n    {\r\n        printf(<span class=\"hljs-string\">\"Bottom(int _a=%d, int _b=%d, int _c=%d, int _d=%d)\\n\"<\/span>, _a, _b, _c, _d);\r\n        d = _d;\r\n    } \r\n    <span class=\"hljs-built_in\">int<\/span> d;\r\n};<\/code><\/pre>\n<p>(\u9996\u5148\u8003\u8651\u975e\u865a\u62df\u7684\u60c5\u51b5\u3002)\u4f60\u4f1a\u671f\u671b\u4e0b\u9762\u7684\u4ee3\u7801\u6bb5\u8f93\u51fa\u4ec0\u4e48\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs perl has-numbering\">Bottom bottom(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">4<\/span>);\r\n<span class=\"hljs-keyword\">printf<\/span>(<span class=\"hljs-string\">\"<span class=\"hljs-variable\">%d<\/span> <span class=\"hljs-variable\">%d<\/span> <span class=\"hljs-variable\">%d<\/span> <span class=\"hljs-variable\">%d<\/span> <span class=\"hljs-variable\">%d<\/span>\\n\"<\/span>, bottom.Left::a, bottom.Right::a,\r\n   bottom.b, bottom.c, bottom.d);<\/code><\/pre>\n<p>\u4f60\u53ef\u80fd\u4f1a\u5e0c\u671b\u5f97\u5230\u4e0b\u9762\u7684\u7ed3\u679c\uff0c\u5e76\u4e14\u4e5f\u5f97\u5230\u4e86\u4e0b\u9762\u7684\u7ed3\u679c\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs  has-numbering\">1 1 2 3 4<\/code><\/pre>\n<p>\u7136\u800c\uff0c\u73b0\u5728\u8003\u8651\u865a\u62df\u7684\u60c5\u51b5(\u6211\u4eec\u865a\u62df\u7ee7\u627f\u81eaTop\u7c7b)\u3002\u5982\u679c\u6211\u4eec\u4ec5\u4ec5\u505a\u90a3\u6837\u4e00\u4e2a\u6539\u53d8\uff0c\u5e76\u518d\u4e00\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u6211\u4eec\u4f1a\u5f97\u5230\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs haml has-numbering\"> -<span class=\"ruby\"><span class=\"hljs-number\">1<\/span> -<span class=\"hljs-number\">1<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-number\">3<\/span> <span class=\"hljs-number\">4<\/span><\/span><\/code><\/pre>\n<p>\u4e3a\u4ec0\u4e48\u5462\uff1f\u901a\u8fc7\u8ddf\u8e2a\u6784\u9020\u51fd\u6570\u7684\u6267\u884c\uff0c\u4f1a\u53d1\u73b0\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs mathematica has-numbering\"><span class=\"hljs-keyword\">Top<\/span>::<span class=\"hljs-keyword\">Top<\/span>()\r\n<span class=\"hljs-keyword\">Left<\/span>::<span class=\"hljs-keyword\">Left<\/span>(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>)\r\n<span class=\"hljs-keyword\">Right<\/span>::<span class=\"hljs-keyword\">Right<\/span>(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">3<\/span>)\r\n<span class=\"hljs-keyword\">Bottom<\/span>::<span class=\"hljs-keyword\">Bottom<\/span>(<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">4<\/span>)<\/code><\/pre>\n<p>\u5c31\u50cf\u4e0a\u9762\u89e3\u91ca\u7684\u4e00\u6837\uff0c\u7f16\u8bd1\u5668\u5728Bottom\u7c7b\u6267\u884c\u5176\u4ed6\u6784\u9020\u51fd\u6570\u4e4b\u524d\u4e2d\u63d2\u5165\u8c03\u7528\u4e86\u7f3a\u7701\u6784\u9020\u51fd\u6570\u3002 \u7136\u540e\uff0c\u5f53Left\u53bb\u8c03\u7528\u5b83\u81ea\u8eab\u7684\u8d85\u7c7b\u7684\u6784\u9020\u51fd\u6570\u65f6(Top),\u6211\u4eec\u4f1a\u53d1\u73b0Top\u5df2\u7ecf\u88ab\u521d\u59cb\u5316\u4e86\u56e0\u6b64\u6784\u9020\u51fd\u6570\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<br \/>\n\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u4f60\u5e94\u8be5\u663e\u793a\u7684\u8c03\u7528\u865a\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs vbscript has-numbering\">Bottom(<span class=\"hljs-built_in\">int<\/span> _a, <span class=\"hljs-built_in\">int<\/span> _b, <span class=\"hljs-built_in\">int<\/span> _c, <span class=\"hljs-built_in\">int<\/span> _d): Top(_a), <span class=\"hljs-built_in\">Left<\/span>(_a,_b), <span class=\"hljs-built_in\">Right<\/span>(_a,_c)\r\n{\r\n   d = _d;\r\n}<\/code><\/pre>\n<h2 id=\"\u6307\u9488\u7b49\u4ef7\"><a name=\"t6\"><\/a><a name=\"t6\"><\/a>\u6307\u9488\u7b49\u4ef7<\/h2>\n<p>\u518d\u5047\u8bbe\u540c\u6837\u7684\uff08\u865a\u62df\uff09\u7c7b\u7ee7\u627f\u7b49\u7ea7\uff0c\u4f60\u5e0c\u671b\u8fd9\u6837\u5c31\u6253\u5370\u201c\u76f8\u7b49\u201d\u5417\uff1f<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs mathematica has-numbering\"><span class=\"hljs-keyword\">Bottom<\/span>* b = new <span class=\"hljs-keyword\">Bottom<\/span>();\r\n<span class=\"hljs-keyword\">Right<\/span>* r = b; if(r == b)\r\n   printf(<span class=\"hljs-string\">\"Equal!\\n\"<\/span>);<\/code><\/pre>\n<p>\u8bb0\u4f4f\u8fd9\u4e24\u4e2a\u5730\u5740\u5e76\u4e0d\u5b9e\u9645\u76f8\u7b49\uff08r\u504f\u79fb\u4e868\u4e2a\u5b57\u8282\uff09\u3002\u4f46\u662f\u8fd9\u5e94\u8be5\u5bf9\u7528\u6237\u5b8c\u5168\u900f\u660e\uff1b\u56e0\u6b64\uff0c\u5b9e\u9645\u4e0a\u7f16\u8bd1\u5668\u5728r\u4e0eb\u6bd4\u8f83\u4e4b\u524d\uff0c\u5c31\u7ed9r\u51cf\u53bb\u4e868\u4e2a\u5b57\u8282\uff1b\u8fd9\u6837\uff0c\u8fd9\u4e24\u4e2a\u5730\u5740\u5c31\u88ab\u8ba4\u4e3a\u662f\u76f8\u7b49\u7684\u4e86\u3002<\/p>\n<h2 id=\"\u8f6c\u6362\u4e3avoid\u7c7b\u578b\u7684\u6307\u9488\"><a name=\"t7\"><\/a><a name=\"t7\"><\/a>\u8f6c\u6362\u4e3avoid\u7c7b\u578b\u7684\u6307\u9488<\/h2>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u6765\u601d\u8003\u4e00\u4e0b\u5f53\u5c06\u4e00\u4e2a\u5bf9\u8c61\u8f6c\u6362\u4e3avoid\u7c7b\u578b\u7684\u6307\u9488\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u4e8b\u60c5\u3002\u7f16\u8bd1\u5668\u5fc5\u987b\u4fdd\u8bc1\u4e00\u4e2a\u6307\u9488\u8f6c\u6362\u4e3avoid\u7c7b\u578b\u7684\u6307\u9488\u65f6\u6307\u5411\u5bf9\u8c61\u7684\u9876\u90e8\u3002\u4f7f\u7528\u865a\u51fd\u6570\u8868\u8fd9\u5f88\u5bb9\u6613\u5b9e\u73b0\u3002\u4f60\u53ef\u80fd\u5df2\u7ecf\u60f3\u5230\u4e86\u6307\u5411top\u57df\u7684\u504f\u79fb\u91cf\u662f\u4ec0\u4e48\u3002\u5b83\u662f\u865a\u51fd\u6570\u6307\u9488\u5230\u5bf9\u8c61\u9876\u90e8\u7684\u504f\u79fb\u91cf\u3002\u56e0\u6b64\uff0c\u8f6c\u5316\u4e3avoid\u7c7b\u578b\u7684\u6307\u9488\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528\u67e5\u8be2\u865a\u51fd\u6570\u8868\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u7136\u800c\u4e00\u5b9a\u8981\u786e\u4fdd\u4f7f\u7528\u52a8\u6001\u7c7b\u578b\u8f6c\u6362\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\"><code class=\"hljs cpp has-numbering\"><span class=\"hljs-keyword\">dynamic_cast<\/span>&lt;<span class=\"hljs-keyword\">void<\/span>*&gt;(b);<\/code><\/pre>\n<h1 id=\"\u53c2\u8003\u6587\u732e\"><a name=\"t8\"><\/a><a name=\"t8\"><\/a>\u53c2\u8003\u6587\u732e<\/h1>\n<p>[1]\u00a0<a href=\"http:\/\/www.codesourcery.com\/\">CodeSourcery<\/a>, \u7279\u522b\u662f<a href=\"http:\/\/www.codesourcery.com\/public\/cxx-abi\/\">C++ ABI Summary<\/a>,\u00a0<a href=\"http:\/\/www.codesourcery.com\/public\/cxx-abi\/abi.html\">Itanium C++ ABI<\/a>(\u4e0d\u8003\u8651\u540d\u5b57\uff0c\u8fd9\u4e9b\u6587\u6863\u662f\u5728\u5e73\u53f0\u65e0\u5173\u7684\u4e0a\u4e0b\u6587\u4e2d\u5f15\u7528\u7684\uff1b\u7279\u522b\u4f4e\uff0c<a href=\"http:\/\/www.codesourcery.com\/public\/cxx-abi\/abi.html#vtable\">structure of the vtable<\/a>s\u7ed9\u51fa\u4e86\u865a\u51fd\u6570\u8868\u7684\u8be6\u7ec6\u4fe1\u606f)\u3002<br \/>\nlibstdc++\u5b9e\u73b0\u7684\u52a8\u6001\u7c7b\u578b\u8f6c\u5316\uff0c\u548c\u540cRTTI\u548c\u547d\u540d\u8c03\u6574\u5b9a\u4e49\u5728\u00a0<a href=\"http:\/\/www.codesourcery.com\/public\/cxx-abi\/gcc-tinfo.cc\">tinfo.cc<\/a>\u4e2d\u3002<br \/>\n[2]<a href=\"http:\/\/gcc.gnu.org\/libstdc++\/\">libstdc++<\/a>\u00a0\u7f51\u7ad9\uff0c\u7279\u522b\u662f\u00a0<a href=\"http:\/\/gcc.gnu.org\/onlinedocs\/libstdc++\/api.html\">C++ Standard Library API<\/a>\u8fd9\u4e00\u7ae0\u8282\u3002<br \/>\n[3]Jan Gray \u5199\u7684<a href=\"http:\/\/www.openrce.org\/articles\/files\/jangrayhood.pdf\">C++: Under the Hood<\/a><br \/>\n[4]Bruce Eckel\u7684Thinking in C++(\u7b2c\u4e8c\u5377) \u7b2c9\u7ae0\u201d\u591a\u91cd\u7ee7\u627f\u201d\u3002 \u4f5c\u8005\u5141\u8bb8\u4e0b\u8f7d\u8fd9\u672c\u4e66<a href=\"http:\/\/mindview.net\/Books\/DownloadSites\">download<\/a>.<\/p>\n<p>\u3010\u8f6c\u81ea\uff1a<a href=\"http:\/\/www.oschina.net\/translate\/cpp-virtual-inheritance\">http:\/\/www.oschina.net\/translate\/cpp-virtual-inheritance<\/a>\u3011<\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 \u8b66\u544a. \u672c\u6587\u6709\u70b9\u6280\u672f\u96be\u5ea6\uff0c\u9700\u8981<\/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":[119],"class_list":["post-4522","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4522","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=4522"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4522\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}