📄 ei13.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 13: List members in an initialization list in the order in which they are declared</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 = "EI13_DIR.HTM";
var dingtext = "Item E13, P";
if (self == top) {
top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<!-- SectionName="E13: Order initialization lists correctly" -->
<A NAME="2117"></A>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI12_FR.HTM" TARGET="_top">Item 12: Prefer initialization to assignment in constructors.</A> <BR> Continue to <A HREF="./EI14_FR.HTM" TARGET="_top">Item 14: Make sure base classes have virtual destructors.</A></FONT></DIV>
<P><A NAME="dingp1"></A><FONT ID="eititle">Item 13: List members in an initialization list in the order in which they are declared.</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>
<A NAME="2118"></A>
<P><A NAME="dingp2"></A>
Unrepentant Pascal and Ada programmers often yearn for the ability to define arrays with arbitrary bounds, i.e., from 10 to 20 instead of from 0 to 10. Long-time C programmers will insist that everybody who's anybody will always start counting from 0, but it's easy enough to placate the <CODE>begin</CODE>/<CODE>end</CODE> crowd. All you have to do is define your own <CODE>Array</CODE> class <NOBR>template:<SCRIPT>create_link(2);</SCRIPT>
</NOBR></P>
<A NAME="2119"></A>
<UL><PRE>template<class T>
class Array {
public:
Array(int lowBound, int highBound);
...
</PRE>
</UL><A NAME="28479"></A>
<UL><PRE>private:
vector<T> data; // the array data is stored
// in a vector object; see
// <A HREF="./EI49_FR.HTM#8392" TARGET="_top">Item 49</A> for info about
// the vector template
</PRE>
</UL><A NAME="16656"></A>
<UL><PRE>
size_t size; // # of elements in array
</PRE>
</UL><A NAME="16657"></A>
<UL><PRE>
int lBound, hBound; // lower bound, higher bound
};
</PRE>
</UL><A NAME="16663"></A>
<UL><PRE>template<class T>
Array<T>::Array(int lowBound, int highBound)
: size(highBound - lowBound + 1),
lBound(lowBound), hBound(highBound),
data(size)
{}
</PRE>
</UL><p><A NAME="2122"></A>
<P><A NAME="dingp3"></A>
An industrial-strength constructor would perform sanity checking on its parameters to ensure that <CODE>highBound</CODE> was at least as great as <CODE>lowBound</CODE>, but there is a much nastier error here: even with perfectly good <A NAME="p58"></A>values for the array's bounds, you have absolutely no idea how many elements <CODE>data</CODE> <NOBR>holds.<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>
<A NAME="2123"></A>
<P><A NAME="dingp4"></A>
"How can that be?" I hear you cry. "I carefully initialized <CODE>size</CODE> before passing it to the <CODE>vector</CODE> constructor!" Unfortunately, you didn't — you just tried to. The rules of the game are that class members are initialized <I>in the order of their declaration in the class</I>; the order in which they are listed in a member initialization list makes not a whit of difference. In the classes generated by your <CODE>Array</CODE> template, <CODE>data</CODE> will always be initialized first, followed by <CODE>size</CODE>, <CODE>lBound</CODE>, and <CODE>hBound</CODE>. <NOBR>Always.<SCRIPT>create_link(4);</SCRIPT>
</NOBR></P>
<A NAME="2124"></A>
<P><A NAME="dingp5"></A>
Perverse though this may seem, there is a reason for it. Consider this <NOBR>scenario:<SCRIPT>create_link(5);</SCRIPT>
</NOBR></P>
<A NAME="2125"></A>
<UL><PRE>class Wacko {
public:
Wacko(const char *s): s1(s), s2(0) {}
Wacko(const Wacko& rhs): s2(rhs.s1), s1(0) {}
</PRE>
</UL><A NAME="28487"></A>
<UL><PRE>private:
string s1, s2;
};
</PRE>
</UL><A NAME="2127"></A>
<UL><PRE>Wacko w1 = "Hello world!";
Wacko w2 = w1;
</PRE>
</UL><A NAME="2128"></A>
<P><A NAME="dingp6"></A>
If members were initialized in the order of their appearance in an initialization list, the data members of <CODE>w1</CODE> and <CODE>w2</CODE> would be constructed in different orders. Recall that the destructors for the members of an object are always called in the inverse order of their constructors. Thus, if the above were allowed, compilers would have to keep track of the order in which the members were initialized for <I>each object</I>, just to ensure that the destructors would be called in the right order. That would be an expensive proposition. To avoid that overhead, the order of construction and destruction is the same for all objects of a given type, and the order of members in an initialization list is <NOBR>ignored.<SCRIPT>create_link(6);</SCRIPT>
</NOBR></P>
<A NAME="2129"></A>
<P><A NAME="dingp7"></A>
Actually, if you really want to get picky about it, only nonstatic data members are initialized according to the rule. Static data members act like global and namespace objects, so they are initialized only once; see <A HREF="./EI47_FR.HTM#8299" TARGET="_top">Item 47</A> for details. Furthermore, base class data members are initialized before derived class data members, so if you're using inheritance, you should list base class initializers at the very beginning of your member initialization lists. (If you're using <I>multiple</I> inheritance, your base classes will be initialized in the order in which you <I>inherit</I> from them; the order in which they're listed in your member initialization lists will again be ignored. However, if you're using multiple inheritance, you've probably got more important things to worry about. If you <A NAME="p59"></A>don't, <A HREF="./EI43_FR.HTM#7778" TARGET="_top">Item 43</A> would be happy to make suggestions regarding aspects of multiple inheritance that are <NOBR>worrisome.)<SCRIPT>create_link(7);</SCRIPT>
</NOBR></P>
<A NAME="2130"></A>
<P><A NAME="dingp8"></A>
The bottom line is this: if you hope to understand what is really going on when your objects are initialized, be sure to list the members in an initialization list in the order in which those members are declared in the <NOBR>class.<SCRIPT>create_link(8);</SCRIPT>
</NOBR></P>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI12_FR.HTM" TARGET="_top">Item 12: Prefer initialization to assignment in constructors.</A> <BR> Continue to <A HREF="./EI14_FR.HTM" TARGET="_top">Item 14: Make sure base classes have virtual destructors.</A></FONT></DIV>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -