亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ei47.htm

?? 有基本了解的程序員或程序愛好者而做
?? 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 47: Ensure that non-local static objects are initialized before they're used</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 = "EI47_DIR.HTM";
var dingtext = "Item E47, P";
if (self == top) {
 top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<!-- SectionName="E47: Initialization of non-local static objects" -->
<A NAME="8299"></A>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI46_FR.HTM" TARGET="_top">Item 46: Prefer compile-time and link-time errors to runtime errors.</A> &nbsp;&nbsp;<BR>&nbsp;&nbsp;Continue to <A HREF="./EI48_FR.HTM" TARGET="_top">Item 48: Pay attention to compiler warnings.</A></FONT></DIV>

<P><A NAME="dingp1"></A><FONT ID="eititle">Item 47: &nbsp;Ensure that non-local static objects are initialized before they're used.</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>

<A NAME="8300"></A>
<P><A NAME="dingp2"></A>
You're an adult now, so you don't need me to tell you it's foolhardy to use an object before it's been initialized. In fact, the whole notion may <A NAME="p220"></A>strike you as absurd; constructors make sure objects are initialized when they're created, <I>n'est-ce pas</I>?<SCRIPT>create_link(2);</SCRIPT>
</P>
<A NAME="8303"></A>
<P><A NAME="dingp3"></A>
Well, yes and no. Within a particular translation unit (i.e., source file), everything works fine, but things get trickier when the initialization of an object in one translation unit depends on the value of another object in a different translation unit <i>and</i> that second object itself requires <NOBR>initialization.<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>
<A NAME="30382"></A>
<P><A NAME="dingp4"></A>
For example, suppose you've authored a library offering an abstraction of a file system, possibly including such capabilities as making files on the Internet look like they're local. Since your library makes the world look like a single file system, you might create a special object, <CODE>theFileSystem</CODE>, within your library's namespace (see <A HREF="./EI28_FR.HTM#6429" TARGET="_top">Item 28</A>) for clients to use whenever they need to interact with the file system abstraction your library <NOBR>provides:<SCRIPT>create_link(4);</SCRIPT>
</NOBR></P>
<A NAME="30547"></A>
<UL><PRE>
class FileSystem { ... };            // this class is in your
                                     // library
</PRE>
</UL><A NAME="30587"></A>
<UL><PRE>
FileSystem theFileSystem;            // this is the object
                                     // with which library
                                     // clients interact
</PRE>
</UL><A NAME="30588"></A>
<P><A NAME="dingp5"></A>
Because <CODE>theFileSystem</CODE> represents something complicated, it's no surprise that its construction is both nontrivial and essential; use of <CODE>theFileSystem</CODE> before it had been constructed would yield <I>very</I> undefined behavior. (However, consult <A HREF="../MEC/MI17_FR.HTM#41011" TARGET="_top">Item M17</A> for ideas on how the effective initialization of objects like <CODE>theFileSystem</CODE> can safely be <NOBR>delayed.)<SCRIPT>create_link(5);</SCRIPT>
</NOBR></P>
<A NAME="30564"></A>
<P><A NAME="dingp6"></A>
Now suppose some client of your library creates a class for directories in a file system. Naturally, their class uses <CODE>theFileSystem</CODE>:<SCRIPT>create_link(6);</SCRIPT>
</P>
<A NAME="30565"></A>
<UL><PRE>
class Directory {                    // created by library client
public:
  Directory();
  ...
};
</PRE>
</UL><A NAME="30566"></A>
<UL><PRE>Directory::Directory()
{
  <I>create a</I> Directory <I>object by invoking member
  functions on</I> theFileSystem;
}
</PRE>
</UL><A NAME="30567"></A>
<P><A NAME="dingp7"></A>
Further suppose this client decides to create a distinguished global <CODE>Directory</CODE> object for temporary <NOBR>files:<SCRIPT>create_link(7);</SCRIPT>
</NOBR></P>
<A NAME="30573"></A>
<UL><PRE>
Directory tempDir;                  // directory for temporary
                                    // files
</PRE>
</UL><A NAME="30574"></A>
<P><A NAME="dingp8"></A>
<A NAME="p221"></A>Now the problem of initialization order becomes apparent: unless <CODE>theFileSystem</CODE> is initialized before <CODE>tempDir</CODE>, <CODE>tempDir</CODE>'s constructor will attempt to use <CODE>theFileSystem</CODE> before it's been initialized. But <CODE>theFileSystem</CODE> and <CODE>tempDir</CODE> were created by different people at different times in different files. How can you be sure that <CODE>theFileSystem</CODE> will be created before <CODE>tempDir</CODE>?<SCRIPT>create_link(8);</SCRIPT>
</P>
<A NAME="8313"></A>
<P><A NAME="dingp9"></A>
This kind of question arises anytime you have <I>non-local static objects</I> that are defined in different translation units and whose correct behavior is dependent on their being initialized in a particular order. Non-local static objects are objects that <NOBR>are<SCRIPT>create_link(9);</SCRIPT>
</NOBR></P>
<UL><A NAME="24055"></A>
<A NAME="dingp10"></A><LI>defined at global or namespace scope (e.g., <CODE>theFileSystem</CODE> and <CODE>tempDir</CODE>),<SCRIPT>create_link(10);</SCRIPT>

<A NAME="30829"></A>
<A NAME="dingp11"></A><LI>declared <CODE>static</CODE> in a class, or<SCRIPT>create_link(11);</SCRIPT>

<A NAME="24056"></A>
<A NAME="dingp12"></A><LI>defined <CODE>static</CODE> at file scope.<SCRIPT>create_link(12);</SCRIPT>

</UL></P>
<A NAME="195248"></A>
<P><A NAME="dingp13"></A>
Regrettably, there is no shorthand term for "non-local static objects," so you should accustom yourself to this somewhat awkward <NOBR>phrase.<SCRIPT>create_link(13);</SCRIPT>
</NOBR></P>
<A NAME="195249"></A>
<P><A NAME="dingp14"></A>
You do not want the behavior of your software to be dependent on the initialization order of non-local static objects in different translation units, because you have no control over that order. Let me repeat that. <I>You have absolutely no control over the order in which non-local static objects in different translation units are initialized.</I><SCRIPT>create_link(14);</SCRIPT>
</P>
<A NAME="24544"></A>
<P><A NAME="dingp15"></A>
It is reasonable to wonder why this is the <NOBR>case.<SCRIPT>create_link(15);</SCRIPT>
</NOBR></P>
<A NAME="223072"></A>
<P><A NAME="dingp16"></A>
It is the case because determining the "proper" order in which to initialize non-local static objects is hard. Very hard. Halting-Problem hard. In its most general form &#151; with multiple translation units and non-local static objects generated through implicit template instantiations (which may themselves arise via implicit template instantiations) &#151; it's not only impossible to determine the right order of initialization, it's typically not even worth looking for special cases where it is possible to determine the right <NOBR>order.<SCRIPT>create_link(16);</SCRIPT>
</NOBR></P>
<A NAME="24419"></A>
<P><A NAME="dingp17"></A>
In the field of Chaos Theory, there is a principle known as the "Butterfly Effect." This principle asserts that the tiny atmospheric disturbance caused by the beating of a butterfly's wings in one part of the world can lead to profound changes in weather patterns in places far distant. Somewhat more rigorously, it asserts that for some types of systems, minute perturbations in inputs can lead to radical changes in <NOBR>outputs.<SCRIPT>create_link(17);</SCRIPT>
</NOBR></P>
<A NAME="24095"></A>
<P><A NAME="dingp18"></A>
The development of software systems can exhibit a Butterfly Effect of its own. Some systems are highly sensitive to the particulars of their requirements, and small changes in requirements can significantly af<A NAME="p222"></A>fect the ease with which a system can be implemented. For example, <A HREF="./EI29_FR.HTM#6490" TARGET="_top">Item 29</A> describes how changing the specification for an implicit conversion from <CODE>String</CODE>-to-<CODE>char*</CODE> to <CODE>String</CODE>-to-<CODE><I>const</I></CODE>-<CODE>char*</CODE> makes it possible to replace a slow or error-prone function with a fast, safe <NOBR>one.<SCRIPT>create_link(18);</SCRIPT>
</NOBR></P>
<A NAME="24585"></A>
<P><A NAME="dingp19"></A>
The problem of ensuring that non-local static objects are initialized before use is similarly sensitive to the details of what you want to achieve. If, instead of demanding access to non-local static objects, you're willing to settle for access to objects that <I>act</I> like non-local static objects (except for the initialization headaches), the hard problem vanishes. In its stead is left a problem so easy to solve, it's hardly worth calling a problem any <NOBR>longer.<SCRIPT>create_link(19);</SCRIPT>
</NOBR></P>
<A NAME="24581"></A>
<P><A NAME="dingp20"></A>
The technique &#151; sometimes known as the <I>Singleton pattern</I> &#151; is simplicity itself. First, you move each non-local static object into its own function, where you declare it <CODE>static</CODE>. Next, you have the function return a reference to the object it contains. Clients call the function instead of referring to the object. In other words, you replace non-local static objects with objects that are <CODE>static</CODE> inside functions. (See also <A HREF="../MEC/MI26_FR.HTM#5350" TARGET="_top">Item M26</A>.)<SCRIPT>create_link(20);</SCRIPT>
</P>
<A NAME="30738"></A>
<P><A NAME="dingp21"></A>
The basis of this approach is the observation that although C++ says next to nothing about when a non-local static object is initialized, it specifies quite precisely when a static object inside a function (i.e. a <I>local</I> static object) is initialized: it's when the object's definition is first encountered during a call to that function. So if you replace direct accesses to non-local static objects with calls to functions that return references to local static objects inside them, you're guaranteed that the references you get back from the functions will refer to initialized objects. As a bonus, if you never call a function emulating a non-local static object, you never incur the cost of constructing and destructing the object, something that can't be said for true non-local static <NOBR>objects.<SCRIPT>create_link(21);</SCRIPT>
</NOBR></P>
<A NAME="30648"></A>
<P><A NAME="dingp22"></A>
Here's the technique applied to both <CODE>theFileSystem</CODE> and <CODE>tempDir</CODE>:<SCRIPT>create_link(22);</SCRIPT>
</P><A NAME="30650"></A>
<UL><PRE>
class FileSystem { ... };            // same as before
</PRE>
</UL><A NAME="30654"></A>
<UL><PRE>
FileSystem&amp; theFileSystem()          // this function replaces
{                                    // the theFileSystem object
</PRE>
</UL><A NAME="30741"></A>
<UL><PRE>
  static FileSystem tfs;             // define and initialize
                                     // a local static object
                                     // (tfs = "the file system")
</PRE>
</UL><A NAME="30655"></A>
<UL><PRE>
  return tfs;                        // return a reference to it
}
</PRE>
</UL><A NAME="30659"></A>
<UL><PRE>
class Directory { ... };             // same as before
</PRE>
</UL><A NAME="30752"></A>
<UL><PRE><A NAME="p223"></A>Directory::Directory()
{
  <I>same as before, except references to</I> theFileSystem <I>are
  replaced by references to</I> theFileSystem<I>();</I>
}
</PRE>
</UL><A NAME="30745"></A>
<UL><PRE>
Directory&amp; tempDir()                 // this function replaces
{                                    // the tempDir object
</PRE>
</UL><A NAME="30755"></A>
<UL><PRE>
  static Directory td;               // define/initialize local
                                     // static object
</PRE>
</UL><A NAME="30794"></A>
<UL><PRE>
  return td;                         // return reference to it
}
</PRE>
</UL><A NAME="30682"></A>
<P><A NAME="dingp23"></A>
Clients of this modified system program exactly as they used to, except they now refer to <CODE>theFileSystem()</CODE> and <CODE>tempDir()</CODE> instead of <CODE>theFileSystem</CODE> and <CODE>tempDir</CODE>. That is, they refer only to functions returning references to those objects, never to the objects <NOBR>themselves.<SCRIPT>create_link(23);</SCRIPT>
</NOBR></P>
<A NAME="24703"></A>
<P><A NAME="dingp24"></A>
The reference-returning functions dictated by this scheme are always simple: define and initialize a local static object on line 1, return it on line 2. That's it. Because they're so simple, you may be tempted to declare them <CODE>inline</CODE>. <A HREF="./EI33_FR.HTM#6729" TARGET="_top">Item 33</A> explains that late-breaking revisions to the C++ language specification make this a perfectly valid implementation strategy, but it also explains why you'll want to confirm your compilers' conformance with this aspect of <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">the</NOBR> standard</A> before putting it to use. If you try it with a compiler not yet in accord with the relevant parts of the standard, you risk getting multiple copies of both the access function and the static object defined within it. That's enough to make a grown programmer <NOBR>cry.<SCRIPT>create_link(24);</SCRIPT>
</NOBR></P>
<A NAME="8375"></A>
<P><A NAME="dingp25"></A>
Now, there's no magic going on here. For this technique to be effective, it must be possible to come up with a reasonable initialization order for your objects. If you set things up such that object A must be initialized before object B, and you also make A's initialization dependent on B's having already been initialized, you are going to get in trouble, and frankly, you deserve it. If you steer shy of such pathological situations, however, the scheme described in this Item should serve you quite <NOBR>nicely.<SCRIPT>create_link(25);</SCRIPT>
</NOBR></P>

<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI46_FR.HTM" TARGET="_top">Item 46: Prefer compile-time and link-time errors to runtime errors.</A> &nbsp;&nbsp;<BR>&nbsp;&nbsp;Continue to <A HREF="./EI48_FR.HTM" TARGET="_top">Item 48: Pay attention to compiler warnings.</A></FONT></DIV>

</BODY>
</HTML>

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产欧美一区二区在线| 久久久一区二区| 粉嫩av亚洲一区二区图片| 日本在线不卡视频| 日韩黄色免费电影| 青青草视频一区| 蜜桃在线一区二区三区| 麻豆成人91精品二区三区| 日本欧美加勒比视频| 一区二区三区高清不卡| 污片在线观看一区二区| 日韩avvvv在线播放| 老司机免费视频一区二区| 久久精品国产久精国产| 国产精品亚洲а∨天堂免在线| 国产伦理精品不卡| 成人免费精品视频| 日本二三区不卡| 欧美一级午夜免费电影| 久久久精品影视| 亚洲天堂成人网| 日韩精品乱码av一区二区| 国产美女视频91| 色噜噜夜夜夜综合网| 91精品国产综合久久福利软件| 久久嫩草精品久久久精品一| 亚洲色欲色欲www| 日本女优在线视频一区二区| 国产成人高清视频| 欧美日韩一区二区电影| 欧美mv和日韩mv的网站| 国产精品丝袜一区| 五月天欧美精品| 成人免费视频app| 欧美精品一二三| 国产精品国产三级国产普通话99| 一区二区三区国产精华| 国产精品一区在线| 欧美综合视频在线观看| 久久久久国产一区二区三区四区| 夜夜精品视频一区二区| 大胆亚洲人体视频| 欧美xxxxxxxx| 亚洲一区欧美一区| 成人久久视频在线观看| 欧美一区二区在线免费播放| 亚洲视频免费看| 国产一区 二区 三区一级| 91精品中文字幕一区二区三区| 国产精品乱人伦| 狠狠色丁香婷婷综合久久片| 91成人网在线| 1024成人网色www| 国产在线视视频有精品| 欧美一区二区三区四区在线观看| 最新日韩av在线| 懂色av中文一区二区三区| 日韩免费一区二区| 天涯成人国产亚洲精品一区av| 99这里只有久久精品视频| 精品国产亚洲在线| 毛片av一区二区| 这里只有精品电影| 五月天久久比比资源色| 欧美日韩成人高清| 五月天久久比比资源色| 欧美性videosxxxxx| 亚洲免费观看高清完整版在线观看| 成人综合婷婷国产精品久久| 久久精品视频免费| 亚洲免费资源在线播放| 日本精品一区二区三区四区的功能| 国产精品免费aⅴ片在线观看| 福利一区福利二区| 国产精品久久久久久久久晋中 | 免费高清在线视频一区·| 欧美性色黄大片手机版| 夜夜操天天操亚洲| 欧美精品一二三区| 免费人成在线不卡| 欧美电影免费观看高清完整版在线观看 | 欧美一区二区在线观看| 日产国产欧美视频一区精品| 日韩欧美一级精品久久| 伦理电影国产精品| 日韩美女视频在线| 国产精品性做久久久久久| 国产肉丝袜一区二区| 成人性生交大片免费看在线播放 | 欧美一区永久视频免费观看| 精品一区二区三区的国产在线播放| 欧美刺激午夜性久久久久久久| 国产真实乱子伦精品视频| 国产精品国产三级国产aⅴ原创| 99re成人在线| 偷拍一区二区三区| 欧美精品一区在线观看| 99这里只有久久精品视频| 天天综合色天天综合| 欧美tickle裸体挠脚心vk| 成人黄色免费短视频| 亚洲6080在线| 国产欧美日韩亚州综合 | 日韩一区二区在线看片| 国产在线精品一区二区夜色| 国产精品国产a| 69久久99精品久久久久婷婷| 国产精品99久久久久久久女警 | 久久亚洲一区二区三区明星换脸| 国产99久久久国产精品免费看| 一区二区在线免费| 久久综合九色综合欧美就去吻| 91视频观看免费| 国产精一区二区三区| 亚洲成人精品一区| 国产午夜精品福利| 8v天堂国产在线一区二区| 国产白丝网站精品污在线入口| 亚洲在线观看免费视频| 久久久亚洲综合| 欧美精三区欧美精三区| av激情综合网| 狠狠色狠狠色综合日日91app| 亚洲网友自拍偷拍| 国产精品高潮呻吟久久| 精品福利一二区| 欧美精品在线观看一区二区| 91猫先生在线| 成人性生交大片免费看视频在线| 久久精品国产77777蜜臀| 亚洲成av人片在线| 亚洲人成伊人成综合网小说| 久久精品视频免费| 精品国产sm最大网站免费看| 欧美日韩国产中文| 色拍拍在线精品视频8848| 粉嫩高潮美女一区二区三区| 久色婷婷小香蕉久久| 日日摸夜夜添夜夜添国产精品| 亚洲免费av在线| 亚洲日本成人在线观看| 中文字幕乱码亚洲精品一区| 久久久精品国产免费观看同学| 欧美成人aa大片| 日韩欧美亚洲国产精品字幕久久久| 欧美日韩成人一区| 欧美乱熟臀69xxxxxx| 欧美日韩一区 二区 三区 久久精品| 97精品久久久久中文字幕 | 亚洲国产精品一区二区尤物区| 国产精品久久久久一区二区三区| 久久久国产精华| 国产欧美精品一区二区色综合 | 日韩精品一区二区三区四区视频| 91精品国产免费久久综合| 在线综合亚洲欧美在线视频| 欧美日韩国产一区| 欧美日韩一区三区四区| 8v天堂国产在线一区二区| 91精品国产色综合久久| 精品国产免费久久| 国产色综合一区| 亚洲色欲色欲www| 亚欧色一区w666天堂| 另类专区欧美蜜桃臀第一页| 国产精品一区二区免费不卡| 成人免费高清在线| 色综合激情五月| 欧美一二三在线| 国产精品色眯眯| 亚洲宅男天堂在线观看无病毒| 欧美一级国产精品| 亚洲综合一二区| 欧美二区在线观看| 开心九九激情九九欧美日韩精美视频电影 | 精品视频免费在线| 欧美一区二区三区人| 久久午夜国产精品| 亚洲欧美在线视频| 日韩不卡在线观看日韩不卡视频| 国产精品69毛片高清亚洲| 成人a区在线观看| 91精品免费在线观看| 欧美激情一区二区在线| 亚洲午夜激情av| 国产精品一区专区| 884aa四虎影成人精品一区| 久久久.com| 天天亚洲美女在线视频| 成人av手机在线观看| 91精品国产品国语在线不卡| 国产精品久久久久久久岛一牛影视 | 国产资源在线一区| 岛国精品在线观看| 欧美亚一区二区| 国产精品色一区二区三区| 另类欧美日韩国产在线| 色吊一区二区三区| 欧美国产日本韩| 奇米精品一区二区三区四区|