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

📄 right4-2.htm

📁 Visual C++面向对象程序设计教程(配套习题资源)
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">是访问类</font>A<font FACE="??ì?,SimSun" LANG="ZH-CN">中的</font>f( 
)<font FACE="??ì?,SimSun" LANG="ZH-CN">,还是访问类</font>B<font FACE="??ì?,SimSun" LANG="ZH-CN">中的</font>f( 
)<font FACE="??ì?,SimSun" LANG="ZH-CN">呢?</font></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">解决的方法可用前面用过的成员名限定法来消除二义性,例如:</font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c1.<font color="#FF6600">A::</font>f( 
    );</font></p>
  </blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">或者</font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c1.<font color="#FF6600">B::</font>f( 
    );</font></p>
  </blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" SIZE="3"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">②</font><font face="??ì?,SimSun"> 
</font></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">情况2:</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">在前例中,类</font>B<font FACE="??ì?,SimSun" LANG="ZH-CN">中有一个成员函数</font>g( 
)<font FACE="??ì?,SimSun" LANG="ZH-CN">,类</font>C<font FACE="??ì?,SimSun" LANG="ZH-CN">中也有一个成员函数</font>g( 
)<font FACE="??ì?,SimSun" LANG="ZH-CN">。这时,</font></font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c1.g( 
    );</font></p>
  </blockquote>
</blockquote>
<font SIZE="3">
<p style="line-height: 120%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">不存在二义性,它是指</font>C::g( 
)<font FACE="??ì?,SimSun" LANG="ZH-CN">,而不是指</font>B::g( )<font FACE="??ì?,SimSun" LANG="ZH-CN">。因为这两个</font>g( 
)<font FACE="??ì?,SimSun" LANG="ZH-CN">函数,一个出现在基类</font>B<font FACE="??ì?,SimSun" LANG="ZH-CN">中,一个出现在派生类</font>C<font FACE="??ì?,SimSun" LANG="ZH-CN">中。在类的派生层次结构中,基类的成员和派生类新增的成员的作用范围不同,是相互包含的两个层,派生类在内层。</font></font></p>
<p style="line-height: 120%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2"><font color="#FF6600">对于在不同的作用域声明的标识符,可见性原则是:</font>如果存在两个或多个具有包含关系的作用域,外层声明的标识符如果在内层没有声明同名标识符,那么它在内层仍可见;如果内层声明了同名标识符,则外层标识符在内层不可见,这时称内层变量覆盖了外层同名变量,这种现象称为<font color="#FF6600">同名覆盖</font>。</font></p>
<p style="line-height: 100%; margin-top: 10; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" SIZE="3"><font face="??ì?,SimSun" lang="ZH-CN" size="2">③</font><font face="??ì?,SimSun"> 
</font></font><font face="??ì?,SimSun" lang="ZH-CN" size="2">情况3:</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">当一个派生类从多个基类派生,而这些基类又有一个共同的基类,则对该基类中说明的成员进行访问时,也可能会出现二义性。例如:</font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class 
    A</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">public:</font></p>
    <blockquote>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      a;</font></p>
    </blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">};</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class 
    B1 : public A</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">private:</font></p>
    <blockquote>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      b1;</font></p>
    </blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">};</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class 
    B2 : public A</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">private:</font></p>
    <blockquote>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      b2;</font></p>
    </blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">};</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">class 
    C : public B1, public B2</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">public:</font></p>
    <blockquote>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      f( );</font></p>
    </blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">private:</font></p>
    <blockquote>
      <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int 
      c;</font></p>
    </blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">};</font></p>
  </blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">已知:</font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">C 
    c1;</font></p>
  </blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">下面的两个访问都有二义性:</font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c1.a;</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c1.A::a;</font></p>
  </blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">而下面的访问是正确的:</font></p>
<blockquote>
  <blockquote>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c1.B1::a;</font></p>
    <p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">c1.B2::a;</font></p>
  </blockquote>
</blockquote>

<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">&nbsp;&nbsp; <span style="position: absolute; left: 32; top: 2269"><a target="_self" href="right4-1.htm"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>&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;&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;&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;&nbsp; <span style="position: absolute; left: 486; top: 2273"><a href="right4-3.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></font></p>       

</body>

</html>

⌨️ 快捷键说明

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