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

📄 ei14.htm

📁 一个非常适合初学者入门的有关c++的文档
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!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 14: Make sure base classes have virtual destructors</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 = "EI14_DIR.HTM";
var dingtext = "Item E14, P";
if (self == top) {
 top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>

</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<!-- SectionName="E14: Make sure base classes have virtual dtors" -->
<A NAME="223029"></A>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI13_FR.HTM" TARGET="_top">Item 13: List members in an initialization list in the order in which they are declared.</A> &nbsp;&nbsp;<BR>&nbsp;&nbsp;Continue to <A HREF="./EI15_FR.HTM" TARGET="_top">Item 15: Have operator= return a reference to *this.</A></FONT></DIV>

<P><A NAME="dingp1"></A><FONT ID="eititle">Item 14: &nbsp;Make sure base classes have virtual destructors.</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>

<P><A NAME="dingp2"></A><A NAME="2132"></A>Sometimes it's convenient for a class to keep track of how many objects of its type exist. The straightforward way to do this is to create a static class member for counting the objects. The member is initialized to 0, is incremented in the class constructors, and is decremented in the class destructor. (<A HREF="../MEC/MI26_FR.HTM#5350" TARGET="_top">Item M26</A> shows how to package this approach so it's easy to add to any class, and <A HREF="../MAGAZINE/CO_FRAME.HTM" TARGET="_top">my article on counting objects</A> describes additional refinements to the <NOBR>technique.)<SCRIPT>create_link(2);</SCRIPT>
</NOBR></P>

<P><A NAME="dingp3"></A><A NAME="2133"></A>You might envision a military application, in which a class representing enemy targets might look something like <NOBR>this:<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>

<A NAME="2134"></A>
<UL><PRE>class EnemyTarget {
public:
  EnemyTarget() { ++numTargets; }
  EnemyTarget(const EnemyTarget&amp;) { ++numTargets; }
  ~EnemyTarget() { --numTargets; }
</PRE>
</UL><A NAME="29358"></A>
<UL><PRE>  static size_t numberOfTargets()
  { return numTargets; }
</PRE>
</UL><A NAME="2137"></A>
<UL><PRE>
  virtual bool destroy();                 // returns success of
                                          // attempt to destroy
                                          // EnemyTarget object
</PRE>
</UL><A NAME="28495"></A>
<UL><PRE>private:
  static size_t numTargets;               // object counter
};
</PRE>
</UL><A NAME="2138"></A>
<UL><PRE>// class statics must be defined outside the class;
// initialization is to 0 by default
size_t EnemyTarget::numTargets;
</PRE>
</UL><A NAME="2139"></A>

<P><A NAME="dingp4"></A>This class is unlikely to win you a government defense contract, but it will suffice for our purposes here, which are substantially less demanding than are those of the Department of Defense. Or so we may <NOBR>hope.<SCRIPT>create_link(4);</SCRIPT>
</NOBR></P>

<P><A NAME="dingp5"></A><A NAME="2140"></A>Let us suppose that a particular kind of enemy target is an enemy tank, which you model, naturally enough (see <A HREF="./EI35_FR.HTM#6914" TARGET="_top">Item 35</A>, but also see <A HREF="../MEC/MI33_FR.HTM#10947" TARGET="_top">Item M33</A>), as a publicly derived class of <CODE>EnemyTarget</CODE>. Because you are interested in the total number of enemy tanks as well as the total <A NAME="p60"></A>number of enemy targets, you'll pull the same trick with the derived class that you did with the base <NOBR>class:<SCRIPT>create_link(5);</SCRIPT>
</NOBR></P>

<A NAME="2141"></A>
<UL><PRE>class EnemyTank: public EnemyTarget {
public:
  EnemyTank() { ++numTanks; }
</PRE>
</UL><A NAME="15156"></A>
<UL><PRE>  EnemyTank(const EnemyTank&amp; rhs)
  : EnemyTarget(rhs)
  { ++numTanks; }
</PRE>
</UL><A NAME="5525"></A>
<UL><PRE>  ~EnemyTank() { --numTanks; }
</PRE>
</UL><A NAME="2143"></A>
<UL><PRE>  static size_t numberOfTanks()
  { return numTanks; }
</PRE>
</UL><A NAME="28501"></A>
<UL><PRE>  virtual bool destroy();
</PRE>
</UL><A NAME="28502"></A>
<UL><PRE>private:
  static size_t numTanks;         // object counter for tanks
};
</PRE>
</UL>
<P><A NAME="dingp6"></A><A NAME="2145"></A>Having now added this code to two different classes, you may be in a better position to appreciate <A HREF="../MEC/MI26_FR.HTM#5350" TARGET="_top">Item M26</A>'s general solution to the <NOBR>problem.<SCRIPT>create_link(6);</SCRIPT>
</NOBR></P>

<P><A NAME="dingp7"></A><A NAME="222037"></A>Finally, let's assume that somewhere in your application, you dynamically create an <CODE>EnemyTank</CODE> object using <CODE>new</CODE>, which you later get rid of via <CODE>delete</CODE>:<SCRIPT>create_link(7);</SCRIPT>
</P>

<A NAME="2146"></A>
<UL><PRE>EnemyTarget *targetPtr = new EnemyTank;
</PRE>
</UL><A NAME="2147"></A>
<UL><PRE>...
</PRE>
</UL><A NAME="2148"></A>
<UL><PRE>delete targetPtr;
</PRE>
</UL>

<P><A NAME="dingp8"></A><A NAME="2149"></A>Everything you've done so far seems completely kosher. Both classes undo in the destructor what they did in the constructor, and there's certainly nothing wrong with your application, in which you were careful to use <CODE>delete</CODE> after you were done with the object you conjured up with <CODE>new</CODE>. Nevertheless, there is something very troubling here. Your program's behavior is <I>undefined</I> &#151; you have no way of knowing what will <NOBR>happen.<SCRIPT>create_link(8);</SCRIPT>
</NOBR></P>

<P><A NAME="dingp9"></A><A NAME="2150"></A>The <NOBR><FONT COLOR="#FF0000" SIZE="-2"><B>&deg;</B></FONT><A HREF="http://www.awl.com/cseng/cgi-bin/cdquery.pl?name=cstandard" onMouseOver = "self.status = 'The latest publicly-available version of the C++ standard'; return true" onMouseOut = "self.status = self.defaultStatus" TARGET="_top">C++</NOBR> language standard</A> is unusually clear on this topic. When you try to delete a derived class object through a base class pointer and the base class has a nonvirtual destructor (as <CODE>EnemyTarget</CODE> does), the results are undefined. That means compilers may generate code to do whatever they like: reformat your disk, send suggestive mail to your boss, fax source code to your competitors, whatever. (What often happens at runtime is that the derived class's destructor is never called. In this example, that would mean your count of <CODE>EnemyTank</CODE>s would not be adjusted when <CODE>targetPtr</CODE> was deleted. Your count of enemy tanks would thus be wrong, a rather disturbing prospect to combatants dependent on accurate battlefield <NOBR>information.)<SCRIPT>create_link(9);</SCRIPT>
</NOBR></P>

<A NAME="2151"></A>

<P><A NAME="dingp10"></A><A NAME="p61"></A>To avoid this problem, you have only to make the <CODE>EnemyTarget</CODE> destructor <I>virtual</I>. Declaring the destructor virtual ensures well-defined behavior that does precisely what you want: both <CODE>EnemyTank</CODE>'s and <CODE>EnemyTarget</CODE>'s destructors will be called before the memory holding the object is <NOBR>deallocated.<SCRIPT>create_link(10);</SCRIPT>
</NOBR></P>

<P><A NAME="dingp11"></A><A NAME="2152"></A>Now, the <CODE>EnemyTarget</CODE> class contains a virtual function, which is generally the case with base classes. After all, the purpose of virtual functions is to allow customization of behavior in derived classes (see <A HREF="./EI36_FR.HTM#7007" TARGET="_top">Item 36</A>), so almost all base classes contain virtual <NOBR>functions.<SCRIPT>create_link(11);</SCRIPT>
</NOBR></P>

<P><A NAME="dingp12"></A><A NAME="2153"></A>If a class does <I>not</I> contain any virtual functions, that is often an indication that it is not meant to be used as a base class. When a class is not intended to be used as a base class, making the destructor virtual is usually a bad idea. Consider this example, based on a discussion in the ARM (see <A HREF="./EI50_FR.HTM#8569" TARGET="_top">Item 50</A>):<SCRIPT>create_link(12);</SCRIPT>
</P>

<A NAME="2154"></A>

⌨️ 快捷键说明

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