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

📄 [24] inheritance private and protected inheritance, c++ faq lite.htm

📁 c++faq。里面有很多关于c++的问题的解答。
💻 HTM
📖 第 1 页 / 共 2 页
字号:
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/strange-inheritance.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/coding-standards.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[24.3]></A>
<DIV class=FaqTitle>
<H3>[24.3] 我应该选谁:组合还是私有继承? <IMG alt=UPDATED! 
src="[24] Inheritance private and protected inheritance, C++ FAQ Lite.files/updated.gif"></H3></DIV><SMALL><EM>[Recently 
changed so it uses new-style headers and the <TT>std::</TT> syntax (on 7/00). <A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.5]">Click 
here to go to the next FAQ in the "chain" of recent changes<!--rawtext:[24.5]:rawtext--></A>.]</EM></SMALL> 
<P>尽可能用组合,万不得已才用私有继承<BR><BR>通常你不会想访问其他类的内部,而私有继承给你这样的一些的特权(和责任)。但是私有继承并不有害。只是由于它增加了别人更改某些东西时,破坏你的代码的可能性,从而使维护的花费更昂贵。<BR><BR>当你要创建一个&nbsp;<TT>Fred</TT>&nbsp;类,它使用了&nbsp;<TT>Wilma</TT>&nbsp;类的代码,并且<TT>Wilma</TT>&nbsp;类的这些代码需要调用你新建的&nbsp;<TT>Fred</TT>&nbsp;类的成员函数。在这种情况下,<TT>Fred</TT>&nbsp;&nbsp;调用&nbsp;<TT>Wilma</TT>&nbsp;的非虚函数,而<TT>Wilma</TT>&nbsp;调用(通常是<A 
href="http://www.sunistudio.com/cppfaq/abcs.html#[22.4]">纯虚函数</A>)被<TT>Fred</TT>重写的这些函数。这种情况,用组合是很难完成的。<BR>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Wilma&nbsp;{<BR>&nbsp;protected:<BR>&nbsp;&nbsp;&nbsp;void&nbsp;fredCallsWilma()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;&lt;&lt;&nbsp;"Wilma::fredCallsWilma()\n";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wilmaCallsFred();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;virtual&nbsp;void&nbsp;wilmaCallsFred()&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/abcs.html#[22.4]">纯虚函数</A></EM><TT><BR>&nbsp;};<BR>&nbsp;<BR>&nbsp;class&nbsp;Fred&nbsp;:&nbsp;private&nbsp;Wilma&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;void&nbsp;barney()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;&lt;&lt;&nbsp;"Fred::barney()\n";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Wilma::fredCallsWilma();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;protected:<BR>&nbsp;&nbsp;&nbsp;virtual&nbsp;void&nbsp;wilmaCallsFred()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;&lt;&lt;&nbsp;"Fred::wilmaCallsFred()\n";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;}; 
</TT></DIV>
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/strange-inheritance.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/coding-standards.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[24.4]></A>
<DIV class=FaqTitle>
<H3>[24.4] 从私有继承类到父类需要指针类型转换吗?</H3></DIV>
<P>一般来说,不。
<P>对于该私有继承类的成员函数或者<A 
href="http://www.sunistudio.com/cppfaq/friends.html">友元</A>来说,和基类的关系是已知的,并且这种从<TT>PrivatelyDer*</TT>&nbsp;到&nbsp;<TT>Base*</TT>&nbsp;(或&nbsp;<TT>PrivatelyDer&amp;</TT>&nbsp;到&nbsp;<TT>Base&amp;</TT>)的向上转换是安全的,不需要也不推荐进行类型转换。<BR><BR>然而,对于该私有继承类(<TT>PrivatelyDer</TT>)的用户来说,应该避免这种不安全的转换。因为它基于<TT>PrivatelyDer</TT>的私有实现,它可以自行改变。
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/strange-inheritance.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/coding-standards.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[24.5]></A>
<DIV class=FaqTitle>
<H3>[24.5] 保护继承和私有继承的关系是什么?<IMG alt=UPDATED! 
src="[24] Inheritance private and protected inheritance, C++ FAQ Lite.files/updated.gif"></H3></DIV><SMALL><EM>[Recently 
renamed "subclass" to "derived class" (on 7/00). <A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.6]">Click 
here to go to the next FAQ in the "chain" of recent changes<!--rawtext:[24.6]:rawtext--></A>.]</EM></SMALL> 
<P>相同点:都允许重写私有/保护基类的<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html">虚函数</A>,都不表明派生类“是一种(a 
kind-of)”基类。
<P>不同点:保护继承允许派生类的派生类知道继承关系。如此,子孙类可以有效的得知祖先类的实现细节。这样既有好处(它允许保护继承的子类使用它和保护基类的关联)也有代价(保护派生类不能在无潜在破坏更深派生类的情况下改变这种关联)。
<P>保护继承使用&nbsp;<TT>:&nbsp;protected</TT>&nbsp;语法:
<P> 
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Car&nbsp;:&nbsp;protected&nbsp;Engine&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;}; 
</TT></DIV>
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/strange-inheritance.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/coding-standards.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[24.6]></A>
<DIV class=FaqTitle>
<H3>[24.6] 私有继承和保护继承的访问规则是什么?<IMG alt=UPDATED! 
src="[24] Inheritance private and protected inheritance, C++ FAQ Lite.files/updated.gif"></H3></DIV><SMALL><EM>[Recently 
renamed "subclass" to "derived class" (on 7/00). <A 
href="http://www.sunistudio.com/cppfaq/coding-standards.html#[25.1]">Click here 
to go to the next FAQ in the "chain" of recent changes<!--rawtext:[25.1]:rawtext--></A>.]</EM></SMALL> 
<P>以这些类为例: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;B&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;</TT><EM>/*...*/</EM><TT>&nbsp;};<BR>&nbsp;class&nbsp;D_priv&nbsp;:&nbsp;private&nbsp;&nbsp;&nbsp;B&nbsp;{&nbsp;</TT><EM>/*...*/</EM><TT>&nbsp;};<BR>&nbsp;class&nbsp;D_prot&nbsp;:&nbsp;protected&nbsp;B&nbsp;{&nbsp;</TT><EM>/*...*/</EM><TT>&nbsp;};<BR>&nbsp;class&nbsp;D_publ&nbsp;:&nbsp;public&nbsp;&nbsp;&nbsp;&nbsp;B&nbsp;{&nbsp;</TT><EM>/*...*/</EM><TT>&nbsp;};<BR>&nbsp;class&nbsp;UserClass&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;B&nbsp;b;&nbsp;</TT><EM>/*...*/</EM><TT>&nbsp;}; 
</TT></DIV>
<P>子类都不能访问&nbsp;<TT>B</TT>&nbsp;的私有部分。在&nbsp;<TT>D_priv</TT>中,B&nbsp;的公有和保护部分都是私有的。在&nbsp;<TT>D_prot</TT>中,<TT>B</TT>&nbsp;的公有和保护部分都是保护的。在&nbsp;<TT>D_publ</TT>&nbsp;中,<TT>B</TT>&nbsp;的公有部分是公有的,<TT>B</TT>&nbsp;的保护部分是保护的(<TT>D_publ</TT>&nbsp;是一种&nbsp;<TT>B</TT>)。<TT>UserClass</TT>&nbsp;类仅仅可以访问&nbsp;<TT>B</TT>&nbsp;的公有部分。<BR><BR>要使&nbsp;<TT>B</TT>&nbsp;的公有成员在&nbsp;<TT>D_priv</TT>&nbsp;或&nbsp;<TT>D_prot</TT>中也是公有的,则使用&nbsp;<TT>B::</TT>&nbsp;前缀声明成员的名称。例如,要使&nbsp;<TT>B::f(int,float)</TT>成员在&nbsp;<TT>D_prot</TT>中公有,应该这样写:<BR>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;D_prot&nbsp;:&nbsp;protected&nbsp;B&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;using&nbsp;B::f;&nbsp;&nbsp;</TT><EM>//&nbsp;注意:&nbsp;不是&nbsp;<TT>using&nbsp;B::f(int,float)</TT></EM><TT><BR>&nbsp;}; 
</TT></DIV>
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/strange-inheritance.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/coding-standards.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=bottom></A><A href="mailto:cline@parashift.com"><IMG height=26 
alt=E-Mail 
src="[24] Inheritance private and protected inheritance, C++ FAQ Lite.files/mbox.gif" 
width=89>&nbsp;E-mail the author</A><BR>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/index.html"><EM>C++ FAQ Lite</EM></A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/index.html#table-of-contents">Table&nbsp;of&nbsp;contents</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/subject-index.html">Subject&nbsp;index</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.1]">About&nbsp;the&nbsp;author</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.2]">&copy;</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/on-line-availability.html#[2.2]">Download&nbsp;your&nbsp;own&nbsp;copy</A>&nbsp;]<BR><SMALL>Revised 
Apr 8, 2001</SMALL> </P></BODY></HTML>

⌨️ 快捷键说明

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