📄 [24] inheritance private and protected inheritance, c++ faq lite.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0057)http://www.sunistudio.com/cppfaq/private-inheritance.html -->
<HTML><HEAD><TITLE>[24] Inheritance private and protected inheritance, C++ FAQ Lite</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-cn>
<META content=private-inheritance.html name=FILENAME>
<META content="[24] Inheritance private and protected inheritance, C++ FAQ Lite"
name=ABSTRACT>
<META content=cline@parashift.com name=OWNER>
<META content="Marshall Cline, cline@parashift.com" name=AUTHOR>
<META content="MSHTML 6.00.2462.0" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId><LINK rev=made
href="mailto:cline@parashift.com"><LINK
href="[24] Inheritance private and protected inheritance, C++ FAQ Lite.files/cpp-faq.css"
type=text/css rel=stylesheet></HEAD>
<BODY>
<H1><A name=top></A>[24] 继承 — 私有继承和保护继承<BR><SMALL><SMALL>(Part of <A
href="http://www.sunistudio.com/cppfaq/index.html"><EM>C++ FAQ Lite</EM></A>, <A
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.2]">Copyright ©
1991-2001</A>, <A href="http://www.parashift.com/" target=OutsideTheFAQ>Marshall
Cline</A>, <A
href="mailto:cline@parashift.com">cline@parashift.com</A>)</SMALL></SMALL></H1>
<P>简体中文版翻译:<A href="http://www.sunistudio.com/nicrosoft">申旻</A>,<A
href="mailto:nicrosoft@sunistudio.com">nicrosoft@sunistudio.com</A>(<A
href="http://www.sunistudio.com/">东日制作室</A>,<A
href="http://www.sunistudio.com/asp/sunidoc.asp">东日文档</A>)</P>
<HR>
<H3>FAQs in section [24]:</H3>
<UL>
<LI><A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.1]">[24.1]
如何表示“私有继承”?</A>
<LI><A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.2]">[24.2]
私有继承和组合(composition)有什么类似?</A> <IMG alt=UPDATED!
src="[24] Inheritance private and protected inheritance, C++ FAQ Lite.files/updated.gif">
<LI><A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.3]">[24.3]
我应该选谁:组合还是私有继承?</A> <IMG alt=UPDATED!
src="[24] Inheritance private and protected inheritance, C++ FAQ Lite.files/updated.gif">
<LI><A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.4]">[24.4]
从私有继承类到父类需要指针类型转换吗?</A>
<LI><A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.5]">[24.5]
保护继承和私有继承的关系是什么?</A> <IMG alt=UPDATED!
src="[24] Inheritance private and protected inheritance, C++ FAQ Lite.files/updated.gif">
<LI><A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.6]">[24.6]
私有继承和保护继承的访问规则是什么?</A> <IMG alt=UPDATED!
src="[24] Inheritance private and protected inheritance, C++ FAQ Lite.files/updated.gif">
</LI></UL>
<P>
<HR>
<P><A name=[24.1]></A>
<DIV class=FaqTitle>
<H3>[24.1] 如何表示“私有继承”?</H3></DIV>
<P>用 <TT>: private</TT> 代替 <TT>: public</TT>,例如
<P>
<DIV
class=CodeBlock><TT> class Foo : private Bar {<BR> public:<BR> </TT><EM>// ...</EM><TT><BR> };
</TT></DIV>
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/strange-inheritance.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/coding-standards.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[24.2]></A>
<DIV class=FaqTitle>
<H3>[24.2] 私有继承和组合(composition)有什么类似?<IMG alt=UPDATED!
src="[24] Inheritance private and protected inheritance, C++ FAQ Lite.files/updated.gif"></H3></DIV><SMALL><EM>[Recently
changed the syntax to <TT>using Engine::start;</TT> and added the sixth
distinction thanks to <A href="mailto:brahms@mindspring.com">Stan Brown</A>;
added the third similarity; added "aggregation" as another synonym; general
wordsmithing (on 4/01). <A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.3]">Click
here to go to the next FAQ in the "chain" of recent changes<!--rawtext:[24.3]:rawtext--></A>.]</EM></SMALL>
<P>私有继承是组合的一种语法上的变形(聚合或者 “有一个”)
<P>例如,“汽车有一个(has-a)引擎”关系可以用单一组合表示为:
<P>
<DIV
class=CodeBlock><TT> class Engine {<BR> public:<BR> Engine(int numCylinders);<BR> void start(); </TT><EM>// Starts this <TT>Engine</TT></EM><TT><BR> };<BR> <BR> class Car {<BR> public:<BR> Car() : e_(8) { } </TT><EM>// Initializes this <TT>Car</TT> with 8 cylinders</EM><TT><BR> void start() { e_.start(); } </TT><EM>// Start this <TT>Car</TT> by starting its <TT>Engine</TT></EM><TT><BR> private:<BR> Engine e_; </TT><EM>// <TT>Car</TT> has-a <TT>Engine</TT></EM><TT><BR> }; </TT></DIV>
<P>同样的“有一个”关系也能用私有继承表示:
<P>
<DIV
class=CodeBlock><TT> class Car : private Engine { </TT><EM>// <TT>Car</TT> has-a <TT>Engine</TT></EM><TT><BR> public:<BR> Car() : Engine(8) { } </TT><EM>// Initializes this <TT>Car</TT> with 8 cylinders</EM><TT><BR> using Engine::start; </TT><EM>// Start this <TT>Car</TT> by starting its <TT>Engine</TT></EM><TT><BR> };
</TT></DIV>
<P>两种形式有很多类似的地方:<BR>
<UL>
<LI>两种情况中,都只有一个 Engine 被确切地包含于 Car 中
<LI>两种情况中,在外部都不能将 <TT>Car*</TT> 转换为 <TT>Engine*</TT>
<LI>两种情况中,<TT>Car</TT>类都有一个<TT>start()</TT>方法,并且都在包含的<TT>Engine</TT>对象中调用<TT>start()</TT>方法。
</LI></UL>
<P>也有一些区别:
<UL>
<LI>如果你想让每个 <TT>Car</TT>都包含若干 <TT>Engine</TT>,那么只能用单一组合的形式
<LI>私有继承形式可能引入不必要的多重继承
<LI>私有继承形式允许 <TT>Car</TT> 的成员将 <TT>Car*</TT> 转换成<TT>Engine*</TT>
<LI>私有继承形式允许访问基类的保护(<TT>protected</TT>)成员
<LI>私有继承形式允许 <TT>Car</TT> 重写 <TT>Engine</TT> 的<A
href="http://www.sunistudio.com/cppfaq/virtual-functions.html">虚函数</A>
<LI>私有继承形式赋予<TT>Car</TT>一个更简洁(20个字符比28个字符)的仅通过
<TT>Engine</TT>调用的<TT>start()</TT>方法 </LI></UL>
<P>注意,私有继承通常用来获得对基类的 protected: 成员的访问,但这只是短期的解决方案(<A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.3]">权宜之计</A>)
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#top">Top</A>
| <A
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -