⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10.3.1b.htm

📁 建立《编译原理网络课程》的目的不仅使学生掌握构造编译程序的原理和技术
💻 HTM
字号:
<html>

<head>
<title>编译原理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link type="text/css" rel="stylesheet" href="../css/specification.css">
</head>

<body>

<table align="right" width="300">
  <tr>
    <td>
    <img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'"
    onclick="vbscript:window.location.href='10.3.1.htm'" ></td>
    <td>
    <img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
    onclick="vbscript:window.location.href='10.4.1.htm'" ></imag></td>
  </tr>
</table>

<p> </p>

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<p>
现在我们把单继承的编译方案扩充到独立的双继承场合,在单继承的情况下(见图10.6),一个对象的视图由两部分组成。第一部分只包含一个虚指针,它作为该对象的第一个成分,该指针指向该类的动态方法表;第二部分是该对象拥有的属性值,它们跟随虚指针放在后面的位置上。在多继承方案中,我们仍然保持这种方式。以程序10.12为例,对于C的每个基类B1和B2,编译器必须能够产生C对象的基类视图。因为B1子对象是处在C对象的开头,因而对于B1,我们仍可以使用单继承的办法,即C对象的B1视图是C视图的开头部分(属性和动态方法表都是这样),对于B2视图,它显然不能也放在C视图的开头,一种自然的想法是将B2视图放在B1视图的后面,包括指向B2动态方法表的指针和B2的属性。最后在C视图中还应该包括C的派生属性。
</p>
<p align = "center">
对于这样的安排,编译器需要知道B2视图在C视图中的偏移量,这个偏移量在编译时很容易确定下来,例如在我们这个例子中,B2视图相对于C视图的偏移量就是B1视图的大小。知道了各子对象的视图位置后,我们就可以象编译单继承一样来编译多继承。不同的是,当编译多继承的动态方法时,例如对于程序10.12中的c.g()调用,虚指针的地址要从拥有该方法的子对象B2的视图中得到,而不一定象单继承那样总是从c的起始地址得到。程序10.12对应的对象实例如图10.13所示。从图中可以看出,对象c的动态方法表是由多张表组合而成的,这是由于多继承视图安排造成的结果。</p>
<table border="1" cellpadding="0" align = "center" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="77%" id="AutoNumber1">
  <tr>
    <td width="100%"><font color="#0000FF">class</font> B1{<font color="#0000FF">int</font> a1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    main()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000FF">virtual</font>
    <font color="#0000FF">void</font> f();};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    {<br>
    <font color="#0000FF">class</font> B2{<font color="#0000FF">int</font> b1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    C c;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000FF">virtual</font>
    <font color="#0000FF">void</font> g();}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c.g();<br>
    <font color="#0000FF">class</font> C:<font color="#0000FF">public</font> B1,<font color="#0000FF">public</font> B2{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000FF">int</font> c1;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000FF">virtual</font>
    <font color="#0000FF">void</font> h();}<br>
 </td>
  </tr>
</table>
<p align = center><b>程序10.12 多继承编译的示意程序</b></p>
<p>
</p>
</td></tr></table>

<p> </p>

<table align="right" width="300">
  <tr>
    <td>
    <img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'"
    onclick="vbscript:window.location.href='10.3.1.htm'" ></td>
    <td>
    <img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
    onclick="vbscript:window.location.href='10.4.1.htm'" ></imag></td>
  </tr>
</table>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -