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

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

📁 c++faq。里面有很多关于c++的问题的解答。
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!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&nbsp;&copy; 
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>:&nbsp;private</TT> 代替 <TT>:&nbsp;public</TT>,例如
<P> 
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Foo&nbsp;:&nbsp;private&nbsp;Bar&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.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&nbsp;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>私有继承是组合的一种语法上的变形(聚合或者&nbsp;“有一个”)
<P>例如,“汽车有一个(has-a)引擎”关系可以用单一组合表示为: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Engine&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;Engine(int&nbsp;numCylinders);<BR>&nbsp;&nbsp;&nbsp;void&nbsp;start();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;Starts&nbsp;this&nbsp;<TT>Engine</TT></EM><TT><BR>&nbsp;};<BR>&nbsp;<BR>&nbsp;class&nbsp;Car&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;Car()&nbsp;:&nbsp;e_(8)&nbsp;{&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;Initializes&nbsp;this&nbsp;<TT>Car</TT>&nbsp;with&nbsp;8&nbsp;cylinders</EM><TT><BR>&nbsp;&nbsp;&nbsp;void&nbsp;start()&nbsp;{&nbsp;e_.start();&nbsp;}&nbsp;&nbsp;</TT><EM>//&nbsp;Start&nbsp;this&nbsp;<TT>Car</TT>&nbsp;by&nbsp;starting&nbsp;its&nbsp;<TT>Engine</TT></EM><TT><BR>&nbsp;private:<BR>&nbsp;&nbsp;&nbsp;Engine&nbsp;e_;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;<TT>Car</TT>&nbsp;has-a&nbsp;<TT>Engine</TT></EM><TT><BR>&nbsp;}; </TT></DIV>
<P>同样的“有一个”关系也能用私有继承表示: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Car&nbsp;:&nbsp;private&nbsp;Engine&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;<TT>Car</TT>&nbsp;has-a&nbsp;<TT>Engine</TT></EM><TT><BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;Car()&nbsp;:&nbsp;Engine(8)&nbsp;{&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;Initializes&nbsp;this&nbsp;<TT>Car</TT>&nbsp;with&nbsp;8&nbsp;cylinders</EM><TT><BR>&nbsp;&nbsp;&nbsp;using&nbsp;Engine::start;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;Start&nbsp;this&nbsp;<TT>Car</TT>&nbsp;by&nbsp;starting&nbsp;its&nbsp;<TT>Engine</TT></EM><TT><BR>&nbsp;}; 
</TT></DIV>
<P>两种形式有很多类似的地方:<BR>
<UL>
  <LI>两种情况中,都只有一个&nbsp;Engine&nbsp;被确切地包含于&nbsp;Car&nbsp;中 
  <LI>两种情况中,在外部都不能将&nbsp;<TT>Car*</TT>&nbsp;转换为&nbsp;<TT>Engine*</TT> 
  <LI>两种情况中,<TT>Car</TT>类都有一个<TT>start()</TT>方法,并且都在包含的<TT>Engine</TT>对象中调用<TT>start()</TT>方法。 
  </LI></UL>
<P>也有一些区别: 
<UL>
  <LI>如果你想让每个&nbsp;<TT>Car</TT>都包含若干&nbsp;<TT>Engine</TT>,那么只能用单一组合的形式 
  <LI>私有继承形式可能引入不必要的多重继承 
  <LI>私有继承形式允许&nbsp;<TT>Car</TT>&nbsp;的成员将&nbsp;<TT>Car*</TT>&nbsp;转换成<TT>Engine*</TT> 

  <LI>私有继承形式允许访问基类的保护(<TT>protected</TT>)成员 
  <LI>私有继承形式允许&nbsp;<TT>Car</TT>&nbsp;重写&nbsp;<TT>Engine</TT>&nbsp;的<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>注意,私有继承通常用来获得对基类的&nbsp;protected:&nbsp;成员的访问,但这只是短期的解决方案(<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#[24.3]">权宜之计</A>) 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html#top">Top</A> 
|&nbsp;<A 

⌨️ 快捷键说明

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