📄 item_021.htm
字号:
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 11">
<meta name=Originator content="Microsoft Word 11">
<link rel=File-List href="filelist-20.xml.css" tppabs="http://www.gesoftfactory.com/developer/cppcodingstandards/Item_021_files/filelist.xml">
<title>21</title>
<!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:Author>SGuy</o:Author>
<o:LastAuthor>Dove</o:LastAuthor>
<o:Revision>2</o:Revision>
<o:TotalTime>162</o:TotalTime>
<o:Created>2005-08-07T04:18:00Z</o:Created>
<o:LastSaved>2005-08-07T04:18:00Z</o:LastSaved>
<o:Pages>1</o:Pages>
<o:Words>122</o:Words>
<o:Characters>702</o:Characters>
<o:Lines>5</o:Lines>
<o:Paragraphs>1</o:Paragraphs>
<o:CharactersWithSpaces>823</o:CharactersWithSpaces>
<o:Version>11.5606</o:Version>
</o:DocumentProperties>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:WordDocument>
<w:GrammarState>Clean</w:GrammarState>
<w:PunctuationKerning/>
<w:DrawingGridVerticalSpacing>7.8 pt</w:DrawingGridVerticalSpacing>
<w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>
<w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:SpaceForUL/>
<w:BalanceSingleByteDoubleByteWidth/>
<w:DoNotLeaveBackslashAlone/>
<w:ULTrailSpace/>
<w:DoNotExpandShiftReturn/>
<w:AdjustLineHeightInTable/>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:UseFELayout/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]-->
<style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
h3
{mso-style-next:Normal;
margin-top:13.0pt;
margin-right:0cm;
margin-bottom:13.0pt;
margin-left:0cm;
text-align:justify;
text-justify:inter-ideograph;
line-height:173%;
mso-pagination:lines-together;
page-break-after:avoid;
mso-outline-level:3;
font-size:16.0pt;
font-family:"Times New Roman";
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]--><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="2050"/>
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1"/>
</o:shapelayout></xml><![endif]-->
</head>
<body lang=ZH-CN style='tab-interval:21.0pt;text-justify-trim:punctuation'>
<div class=Section1 style='layout-grid:15.6pt'>
<h3><span lang=EN-US style='font-size:13.0pt;line-height:173%;font-family:Arial'>21.
</span><span style='font-size:13.0pt;line-height:173%;font-family:宋体;
mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>避免不同的编译单元在初始化过程中的依赖关系</span><span
lang=EN-US style='font-size:13.0pt;line-height:173%'><o:p></o:p></span></h3>
<p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span
style='font-size:12.0pt;font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>摘要</span></b><b style='mso-bidi-font-weight:
normal'><span lang=EN-US style='font-size:12.0pt'><o:p></o:p></span></b></p>
<p class=MsoNormal><span lang=EN-US style='font-size:12.0pt'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:12.0pt;font-family:宋体;mso-ascii-font-family:
"Times New Roman";mso-hansi-font-family:"Times New Roman"'>保持(初始化的)先后次序:在不同的编译单元中的名字空间层级的对象在初始化时不应该互相依赖,因为它们的初始化次序是未定义的。这样做会导致许多麻烦,轻至在项目中进行微小改动而引发神秘崩溃,重至即使对同一编译器的新版本都不具备可移植性。</span><span
lang=EN-US style='font-size:12.0pt'><o:p></o:p></span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:12.0pt'><o:p> </o:p></span></p>
<p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span
style='font-size:12.0pt;font-family:宋体;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New Roman"'>讨论</span></b><b style='mso-bidi-font-weight:
normal'><span lang=EN-US style='font-size:12.0pt'><o:p></o:p></span></b></p>
<p class=MsoNormal><span lang=EN-US style='font-size:12.0pt'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:12.0pt;font-family:宋体;mso-ascii-font-family:
"Times New Roman";mso-hansi-font-family:"Times New Roman"'>当你在不同的编译单元中定义两个名字空间层级的对象时,哪个对象的构造函数先被调用是未定义的。你的工具可能经常(但并不总是)会以编译单元的目标文件(</span><span
lang=EN-US style='font-size:12.0pt'>object file</span><span style='font-size:
12.0pt;font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>)被链接的次序初始化对象,但这种假设通常并不可靠;即使该假设确实成立,你也不会希望代码的正确与否要微妙地取决于</span><span
lang=EN-US style='font-size:12.0pt'>makefile</span><span style='font-size:12.0pt;
font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>或项目文件。(更多有关次序依赖性的坏处,请参见第</span><span lang=EN-US
style='font-size:12.0pt'>59</span><span style='font-size:12.0pt;font-family:
宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>条)</span><span
lang=EN-US style='font-size:12.0pt'><o:p></o:p></span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:12.0pt'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:12.0pt;font-family:宋体;mso-ascii-font-family:
"Times New Roman";mso-hansi-font-family:"Times New Roman"'>因此,在对任何名字空间层级的对象进行初始化的代码中,你无法假定位于不同的编译单元中的其它对象已经完成了初始化。这些考量对动态初始化的原始类型的变量也同样适用,如</span><span
lang=EN-US style='font-size:12.0pt'>bool reg_success =
LibRegister("mylib");<o:p></o:p></span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:12.0pt'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:12.0pt;font-family:宋体;mso-ascii-font-family:
"Times New Roman";mso-hansi-font-family:"Times New Roman"'>注意,名字空间层级的对象早在被构造函数构造之前,已经被静态地初始化为零(自动对象则与之相反,它们最初会包含无用数据)。荒谬的是,这种清零初始化会使错虫难以检测,因为静态清零初始化使得尚未初始化的对象表面看上去正常,而不是使程序很快地崩溃。你会以为那个字符串为空,那个指针为空,那个整数为零,而事实上你的代码根本还没有对它们进行初始化。</span><span
lang=EN-US style='font-size:12.0pt'><o:p></o:p></span></p>
<p class=MsoNormal><span lang=EN-US style='font-size:12.0pt'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:12.0pt;font-family:宋体;mso-ascii-font-family:
"Times New Roman";mso-hansi-font-family:"Times New Roman"'>为了避免这个问题,应该尽可能地避免名字空间层级的变量,它们很危险(参见第</span><span
lang=EN-US style='font-size:12.0pt'>10</span><span style='font-size:12.0pt;
font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>条)。如果你确实需要此类变量,它们可能会依赖于其它变量,那么请考虑使用</span><span lang=EN-US
style='font-size:12.0pt'>Singleton</span><span style='font-size:12.0pt;
font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>设计模式;小心使用的话,也许能避免隐含的依赖性,因为</span><span lang=EN-US
style='font-size:12.0pt'>Singleton</span><span style='font-size:12.0pt;
font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>模式会确保对象在第一次访问时被初始化。然而,</span><span lang=EN-US
style='font-size:12.0pt'>Singleton</span><span style='font-size:12.0pt;
font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:
"Times New Roman"'>仍是一个披着羊皮的全局变量(参见请</span><span lang=EN-US style='font-size:
12.0pt'>10</span><span style='font-size:12.0pt;font-family:宋体;mso-ascii-font-family:
"Times New Roman";mso-hansi-font-family:"Times New Roman"'>条),并且会被相互的或循环的依赖性破坏(再次重申,清零初始化只会增加混淆)。</span><span
lang=EN-US style='font-size:12.0pt'><o:p></o:p></span></p>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -