📄 ei44.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN" "http://www.w3.org/TR/REC-html40/frameset.dtd">
<HTML LANG="EN">
<HEAD>
<title>Effective C++, 2E | Item 44: Say what you mean; understand what you're saying</TITLE>
<LINK REL=STYLESHEET HREF=../INTRO/ECMEC.CSS>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/COOKIE.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">var imagemax = 0; setCurrentMax(0);</SCRIPT>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/DINGBATS.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">
var dingbase = "EI44_DIR.HTM";
var dingtext = "Item E44, P";
if (self == top) {
top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<!-- SectionName="E44: Say what you mean, understand what you say" -->
<A NAME="8113"></A>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI43_FR.HTM" TARGET="_top">Item 43: Use multiple inheritance judiciously. </A> <BR> Continue to <A HREF="./EMISC_FR.HTM" TARGET="_top">Miscellany</A></FONT></DIV>
<P><A NAME="dingp1"></A><A NAME="p210"></A><FONT ID="eititle">Item 44: Say what you mean; understand what you're saying.</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>
<A NAME="8114"></A>
<P><A NAME="dingp2"></A>
In the introduction to this section on inheritance and object-oriented design, I emphasized the importance of understanding what different object-oriented constructs in C++ <I>mean</I>. This is quite different from just knowing the rules of the language. For example, the rules of C++ say that if class <CODE>D</CODE> publicly inherits from class <CODE>B</CODE>, there is a standard conversion from a <CODE>D</CODE> pointer to a <CODE>B</CODE> pointer; that the public member functions of <CODE>B</CODE> are inherited as public member functions of <CODE>D</CODE>, etc. That's all true, but it's close to useless if you're trying to translate your design into C++. Instead, you need to understand that public inheritance means isa, that if <CODE>D</CODE> publicly inherits from <CODE>B</CODE>, every object of type <CODE>D</CODE> isa object of type <CODE>B</CODE>, too. Thus, if you mean isa in your design, you know you have to use public <NOBR>inheritance.<SCRIPT>create_link(2);</SCRIPT>
</NOBR></P>
<A NAME="8115"></A>
<P><A NAME="dingp3"></A>
Saying what you mean is only half the battle. The flip side of the coin is understanding what you're saying, and it's just as important. For example, it's irresponsible, if not downright immoral, to run around declaring member functions nonvirtual without recognizing that in so doing you are imposing constraints on subclasses. In declaring a nonvirtual member function, what you're really saying is that the function represents an invariant over specialization, and it would be disastrous if you didn't know <NOBR>that.<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>
<A NAME="22939"></A>
<P><A NAME="dingp4"></A>
The equivalence of public inheritance and isa, and of nonvirtual member functions and invariance over specialization, are examples of how certain C++ constructs correspond to design-level ideas. The list below summarizes the most important of these <NOBR>mappings.<SCRIPT>create_link(4);</SCRIPT>
</NOBR></P>
<UL><A NAME="22941"></A>
<A NAME="dingp5"></A><LI><B>A common base class means common traits.</B> If class <CODE>D1</CODE> and class <CODE>D2</CODE> both declare class <CODE>B</CODE> as a base, <CODE>D1</CODE> and <CODE>D2</CODE> inherit common data members and/or common member functions from <CODE>B</CODE>. See <A HREF="./EI43_FR.HTM#7778" TARGET="_top">Item 43</A>.<SCRIPT>create_link(5);</SCRIPT>
<A NAME="8123"></A>
<A NAME="dingp6"></A><LI><B>Public inheritance means isa.</B> If class <CODE>D</CODE> publicly inherits from class <CODE>B</CODE>, every object of type <CODE>D</CODE> is also an object of type <CODE>B</CODE>, but not vice versa. See <A HREF="./EI35_FR.HTM#6914" TARGET="_top">Item 35</A>.<SCRIPT>create_link(6);</SCRIPT>
<A NAME="8128"></A>
<A NAME="dingp7"></A><LI><B>Private inheritance means is-implemented-in-terms-of.</B> If class <CODE>D</CODE> privately inherits from class <CODE>B</CODE>, objects of type <CODE>D</CODE> are simply implemented in terms of objects of type <CODE>B</CODE>; no conceptual relationship exists between objects of types <CODE>B</CODE> and <CODE>D</CODE>. See <A HREF="./EI42_FR.HTM#21052" TARGET="_top">Item 42</A>.<SCRIPT>create_link(7);</SCRIPT>
<A NAME="8133"></A>
<A NAME="dingp8"></A><LI><A NAME="p211"></A><B>Layering means has-a or is-implemented-in-terms-of.</B> If class <CODE>A</CODE> contains a data member of type <CODE>B</CODE>, objects of type <CODE>A</CODE> either have a component of type <CODE>B</CODE> or are implemented in terms of objects of type <CODE>B</CODE>. See <A HREF="./EI40_FR.HTM#7424" TARGET="_top">Item 40</A>.<SCRIPT>create_link(8);</SCRIPT>
</UL></P>
<A NAME="8137"></A>
<P><A NAME="dingp9"></A>
The following mappings apply only when public inheritance is <NOBR>involved:<SCRIPT>create_link(9);</SCRIPT>
</NOBR></P>
<A NAME="8139"></A>
<UL><A NAME="dingp10"></A><LI><B>A pure virtual function means that only the function's interface is inherited.</B> If a class <CODE>C</CODE> declares a pure virtual member function <CODE>mf</CODE>, subclasses of <CODE>C</CODE> must inherit the interface for <CODE>mf</CODE>, and concrete subclasses of <CODE>C</CODE> must supply their own implementations for it. See <A HREF="./EI36_FR.HTM#7007" TARGET="_top">Item 36</A>.<SCRIPT>create_link(10);</SCRIPT>
<A NAME="8144"></A>
<A NAME="dingp11"></A><LI><B>A simple virtual function means that the function's interface plus a default implementation is inherited.</B> If a class <CODE>C</CODE> declares a simple (not pure) virtual function <CODE>mf</CODE>, subclasses of <CODE>C</CODE> must inherit the interface for <CODE>mf</CODE>, and they may also inherit a default implementation, if they choose. See <A HREF="./EI36_FR.HTM#7007" TARGET="_top">Item 36</A>.<SCRIPT>create_link(11);</SCRIPT>
<A NAME="8149"></A>
<A NAME="dingp12"></A><LI><B>A nonvirtual function means that the function's interface plus a mandatory implementation is inherited.</B> If a class <CODE>C</CODE> declares a nonvirtual member function <CODE>mf</CODE>, subclasses of <CODE>C</CODE> must inherit both the interface for <CODE>mf</CODE> and its implementation. In effect, <CODE>mf</CODE> defines an invariant over specialization of <CODE>C</CODE>. See <A HREF="./EI36_FR.HTM#7007" TARGET="_top">Item 36</A>.<SCRIPT>create_link(12);</SCRIPT>
</UL></P>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI43_FR.HTM" TARGET="_top">Item 43: Use multiple inheritance judiciously. </A> <BR> Continue to <A HREF="./EMISC_FR.HTM" TARGET="_top">Miscellany</A></FONT></DIV>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -