﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-菜鸟学c++-随笔分类-c++</title><link>http://www.cppblog.com/baggio1984/category/5276.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 21 May 2008 12:17:18 GMT</lastBuildDate><pubDate>Wed, 21 May 2008 12:17:18 GMT</pubDate><ttl>60</ttl><item><title>构造函数初始化列表</title><link>http://www.cppblog.com/baggio1984/archive/2007/09/30/33270.html</link><dc:creator>baggio</dc:creator><author>baggio</author><pubDate>Sun, 30 Sep 2007 14:01:00 GMT</pubDate><guid>http://www.cppblog.com/baggio1984/archive/2007/09/30/33270.html</guid><wfw:comment>http://www.cppblog.com/baggio1984/comments/33270.html</wfw:comment><comments>http://www.cppblog.com/baggio1984/archive/2007/09/30/33270.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/baggio1984/comments/commentRss/33270.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/baggio1984/services/trackbacks/33270.html</trackback:ping><description><![CDATA[<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">构造函数初始化列表以一个冒号开始，接着是以逗号分隔的数据成员列表，每个数据成员后面跟一个放在括号中的初始化式。</span></strong><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例如：</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US>Example::Example() : ival(0), dval(0.0) {}//ival </span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span><span lang=EN-US>dval</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是类的两个数据成员</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">上面的例子和下面不用初始化列表的构造函数看似没什么区别：</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US>Example::Example()</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US>{</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ival = 0;</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>dval = 0.0;</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US>}</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的确，这两个构造函数的结果是一样的。但区别在于：上面的构造函数（使用初始化列表的构造函数）<strong style="mso-bidi-font-weight: normal">显示的初始化类的成员</strong>；而没使用初始化列表的构造函数是<strong style="mso-bidi-font-weight: normal">对类的成员赋值</strong>，<strong style="mso-bidi-font-weight: normal">并没有进行显示的初始化</strong>。</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">初始化和赋值对<span style="COLOR: red">内置类型的成员</span>没有什么大的区别</span></strong><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，像上面的任一个构造函数都可以。但<strong style="mso-bidi-font-weight: normal">有的时候必须用带有初始化列表的构造函数</strong>：</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 36pt; TEXT-INDENT: -36pt; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt"><span lang=EN-US style="mso-bidi-font-family: 宋体"><span style="mso-list: Ignore">（1）<span style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><strong style="mso-bidi-font-weight: normal"><span style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">成员类型是没有默认构造函数的类</span></strong><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。若没有提供显示初始化式，则编译器隐式使用成员类型的默认构造函数，若类没有默认构造函数，则编译器尝试使用默认构造函数将会失败。</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt 36pt; TEXT-INDENT: -36pt; mso-list: l0 level1 lfo1; tab-stops: list 36.0pt"><span lang=EN-US style="mso-bidi-font-family: 宋体"><span style="mso-list: Ignore">（2）<span style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><strong style="mso-bidi-font-weight: normal"><span lang=EN-US style="COLOR: red">const</span></strong><strong style="mso-bidi-font-weight: normal"><span style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">成员或引用类型的成员</span></strong><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。因为</span><span lang=EN-US>const</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对象或引用类型只能初始化，不能对他们赋值。</span></p>
<p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><span lang=EN-US><span style="mso-spacerun: yes">&nbsp;</span></span></p>
<img src ="http://www.cppblog.com/baggio1984/aggbug/33270.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/baggio1984/" target="_blank">baggio</a> 2007-09-30 22:01 <a href="http://www.cppblog.com/baggio1984/archive/2007/09/30/33270.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>