📄 10.2.2.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.2.1.htm'" ></td>
<td>
<img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='10.2.2b.htm'" ></imag></td>
</tr>
</table>
<p> </p>
<font class="title2"><b>10.2.2 方法</b></font>
<table><tr><td>    </td>
<td class="content">
<p>
方法的编译同普通函数的编译很相似:它被编译变换成机器代码存于指令空间的一个特定地址中。例如在程序10.1中类Square的方法perimeter会对应一个机器指令级的标号Square_perimeter作为它的函数入口处。在编译的语义分析阶段,每一个变量的环境域会包含一个指针指向它的类描述符;每个类描述符又包含一个指向它父类的指针和自身的方法实例列表;每个方法实例又包含一个对应的机器指令级的标号。</p>
</td></tr></table>
<br>
<table><tr><td>    </td>
<td class="content">
<p>
<font class = "emphasize">静态方法</font>
<p>静态方法是指在编译时就可以确定下来执行函数体的方法。在c++中没有特殊标明的方法都属于此类。例如在程序10.4中当我们调用一个方法c.f()时,编译出的机器代码是依据变量c的类型来定的,而不是依据c所指向的对象类型来定的,这样静态方法对应的执行函数体就可以在编译时确定下来。为了编译类似于c.f()的方法调用,编译器首先要找到c所对应的类描述符,假设为C;接着要在C中查找方法f,假设没有找到;然后编译器再查找C的父类B,然后在B中寻找方法f,以此类推。假设最后在某个祖先类A中找到了静态方法f,这时编译器就可以将c.f()这个调用编译为机器标号A_f。
</p>
<table border="1" cellpadding="0" align = "center" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="41%" id="AutoNumber1">
<tr>
<td width="100%" align="left">
<font color="#0000FF">class</font> A <br>
{<br>
<font color="#0000FF">int</font> x;<br>
<font color="#0000FF">void</font> f();<br>
};<br>
<font color="#0000FF">class</font> B : <font color="#0000FF">public</font> A
<br>
{<br>
<font color="#0000FF">void</font> g();<br>
};<br>
<font color="#0000FF">class</font> C : <font color="#0000FF">public</font> B
<br>
{<br>
<font color="#0000FF">void</font> g();<br>
};<br>
<font color="#0000FF">class</font> D : <font color="#0000FF">public</font> C<br>
{<br>
<font color="#0000FF">int</font> y;<br>
<font color="#0000FF">void</font> f();<br>
};</td>
</tr>
</table>
<p align=center><b><span style="font-size: 10.5pt; font-family: 宋体">程序</span><span lang="EN-US" style="font-size: 10.5pt; font-family: Times New Roman">10.4
</span><span style="font-size: 10.5pt; font-family: 宋体">编译静态方法的示意程序</span></b></p>
</td></tr></table>
<br><br>
<table align="right" width="300">
<tr>
<td>
<img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='10.2.1.htm'" ></td>
<td>
<img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'"
onclick="vbscript:window.location.href='10.2.2b.htm'" ></imag></td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -