青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

asm, c, c++ are my all
-- Core In Computer
posts - 139,  comments - 123,  trackbacks - 0

/********************************************\
|????歡迎轉載, 但請保留作者姓名和原文鏈接, 祝您進步并共勉!???? |
\********************************************/


C++對象模型(7) -? Member Initialization List

作者: Jerry Cat
時間: 2006/05/12
鏈接:?
http://m.shnenglu.com/jerysun0818/archive/2006/05/12/6978.html


2.4 Member Initialization List
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

When you write a constructor, you have the option of initializing class members either through the
member initialization list or within the body of the constructor. Except in four cases, which one
you choose is not significant.

In this section, I first clarify when use of the initialization list is "significant" and then
explain what actually gets done with that list internally. I then look at a number of possible,
subtle pitfalls.

You must use the member initialization list in the following cases in order for your program to compile:

(1). When initializing a reference member
(2). When initializing a const member
(3). When invoking a base or member class constructor with a set of arguments
???? 低效的第四種情況
In the fourth case, the program compiles and executes correctly. But it does so inefficiently.
For example, given

class Word {
?? String _name;
?? int _cnt;
public:
?? // not wrong, just naive ...
?? Word() {
????? _name = 0;
????? _cnt = 0;
?? }
};
this implementation of the Word constructor initializes _name once, then overrides the
initialization with an assignment, resulting in the creation and the destruction of a temporary
String object. Was this intentional? Unlikely. Does the compiler generate a warning? I'm not aware
of any that does. Here is the likely internal augmentation of this constructor:
// Pseudo C++ Code
Word::Word( /* this pointer goes here */ )
{
?? _name.String::String();???????? // invoke default String constructor
?? String temp = String( 0 );????? // generate temporary
?? _name.String::operator=( temp );// memberwise copy _name
?? temp.String::~String();???????? // destroy temporary
?? _cnt = 0;
}

Had the code been reviewed by the project and corrected, a significantly more efficient
implementation would have been coded:
// preferred implementation
Word::Word : _name( 0 )
{
?? _cnt = 0;
}
This expands to something like this:

// Pseudo C++ Code
Word::Word( /* this pointer goes here */ )
{?? // invoke String( int ) constructor
?? _name.String::String( 0 );
?? _cnt = 0;
}
This pitfall, by the way, is most likely to occur in template code of this form:

template < class type >
foo< type >::foo( type t )
{
?? // may or may not be a good idea depending on the actual type of type
?? _t = t;
}
This has led some programmers to insist rather aggressively that all member initialization be done
within the member initialization list, even the initialization of a well-behaved member such as _cnt:

// some insist on this coding style, 順序有問題!
Word::Word() : _cnt( 0 ), _name( 0 )
{}

Actually, there is a subtlety to note here: The order in which the list entries are set down is
determined by the declaration order of the members within the class declaration, not the order
within the initialization list. In this case, _name is declared before _cnt in Word and so is placed first.

This apparent anomaly between initialization order and order within the initialization list can
lead to the following nasty pitfall:

class X {
?? int i;
?? int j;
public:
?? // oops!? do you see the problem?
?? X( int val ) : j( val ), i( j )
?? {}
?? ...
};

// preferred idiom, 解決咯
X::X( int val ) : j( val )
{
?? i = j;
}

Here is an interesting question: Are the entries in the initialization list entered such that the
declaration order of the class is preserved? That is, given

// An interesting question is asked:
X::X( int val ) : j( val )
{
?? i = j;
}
is the initialization of j inserted before or after the explicit user assignment of j to i? If
the declaration order is preserved, this code fails badly. The code is correct, however, 這才是
真正的原因 - because the initialization list entries are placed before explicit user code.
所以成員初始化不是一股腦兒都放到初始化列表里才是最優方案!

Another common question is whether you can invoke a member function to initialize a member, such as
// is the invocation of X::xfoo() ok?? 問得好!
X::X( int val ) : i( xfoo( val )), j( val )
{}

where xfoo() is a member function of X. The answer is yes, but…. To answer the "but" first, I
reiterate my advice to initialize one member with another inside the constructor body, not in the
member initialization list. You don't know the dependencies xfoo() has regarding the state of the
X object to which it is bound. 還是那句話: 別將所有的成員初始化工作全放在構造函數的初始化列表里 -
By placing xfoo() within the constructor body, you can ensure there is no ambiguity about which
members are initialized at the point of its invocation.

The use of the member function is valid (apart from the issue of whether the members it accesses
have been initialized). This is because the this pointer associated with the object being
constructed is well formed and the expansion simply takes a form like the following:

// Pseudo C++ Code: constructor augmentation
X::X( /* this pointer, */ int val )//一般都將this指針缺省, 但它的確是存在的, 至少對編譯器而言
{
?? i = this->xfoo( val );
?? j = val;
}

where xfoo() is a member function of X. The answer is yes, but…. To answer the "but" first, I
reiterate my advice to initialize one member with another inside the constructor body, not in the
member initialization list. You don't know the dependencies xfoo() has regarding the state of the
X object to which it is bound. By placing xfoo() within the constructor body, you can ensure
there is no ambiguity about which members are initialized at the point of its invocation.

The use of the member function is valid (apart from the issue of whether the members it accesses
have been initialized). This is because the this pointer associated with the object being
constructed is well formed and the expansion simply takes a form like the following:
// Pseudo C++ Code: constructor augmentation
X::X( /* this pointer, */ int val )
{
?? i = this->xfoo( val );
?? j = val;
}

In summary, the compiler iterates over and possibly reorders the initialization list to reflect
the declaration order of the members. It inserts the code within the body of the constructor
prior to any explicit user code. 成員初始化列表的內容"插"在構造函數的最前端.

posted on 2006-05-12 00:49 Jerry Cat 閱讀(845) 評論(0)  編輯 收藏 引用

<2006年5月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用鏈接

留言簿(7)

隨筆檔案

最新隨筆

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            在线成人免费视频| 久久国内精品视频| 久久久久五月天| 午夜精品福利在线观看| 欧美日韩一区在线观看视频| 老鸭窝毛片一区二区三区| 国产精品日日做人人爱 | 亚洲午夜视频在线| 免费在线播放第一区高清av| 久久青草福利网站| 国产一区视频在线看| 欧美一区二区免费视频| 香蕉成人伊视频在线观看 | 国产精品日韩久久久久| 一本大道av伊人久久综合| 日韩午夜电影在线观看| 欧美r片在线| 亚洲高清视频一区二区| 影音先锋日韩资源| 久久嫩草精品久久久精品一| 久久亚洲不卡| 亚洲国产成人精品视频| 老司机一区二区三区| 嫩模写真一区二区三区三州| 一区视频在线| 免费成人在线观看视频| 亚洲国产精品一区二区尤物区| 亚洲国产精品ⅴa在线观看 | 在线欧美视频| 欧美国产在线观看| 日韩视频精品在线| 男女av一区三区二区色多| 欧美.www| 亚洲视频网站在线观看| 国产精品视频免费| 久久精品免费播放| 91久久精品久久国产性色也91| 一本色道久久综合亚洲精品不 | 一区二区三区无毛| 欧美91精品| 一区二区冒白浆视频| 久久成人综合视频| 尤物网精品视频| 欧美美女bb生活片| 亚洲专区欧美专区| 欧美成人精品在线观看| 中文av一区二区| 国产日韩亚洲| 欧美精品v国产精品v日韩精品| 99精品99| 美国三级日本三级久久99| 亚洲免费电影在线观看| 国产精品揄拍一区二区| 久久综合五月| 亚洲一区影院| 亚洲国产精品www| 小处雏高清一区二区三区| 影音先锋中文字幕一区| 欧美日韩视频专区在线播放| 午夜精品av| 亚洲精选中文字幕| 欧美va天堂在线| 欧美一区二区三区免费视频| 最新亚洲一区| 国模精品一区二区三区| 欧美日韩国产不卡| 久久这里有精品15一区二区三区| 日韩一级片网址| 欧美激情一二区| 久久国产精品一区二区三区| 99精品视频免费观看视频| 狠狠网亚洲精品| 国产乱码精品一区二区三区五月婷 | 亚洲香蕉伊综合在人在线视看| 免费一区视频| 久久精品国产69国产精品亚洲| 一本大道久久a久久精品综合 | 好看不卡的中文字幕| 欧美色图首页| 欧美精品久久天天躁| 久久不射网站| 亚洲一区欧美激情| 一本到12不卡视频在线dvd | 女生裸体视频一区二区三区| 亚洲欧美一区二区三区极速播放| 亚洲精品久久久久久下一站| 国内精品视频在线观看| 国产精品乱码久久久久久| 欧美激情久久久久久| 美日韩精品视频免费看| 久久久久九九视频| 欧美影院午夜播放| 亚洲欧美日韩国产综合| 宅男噜噜噜66一区二区66| 亚洲美女色禁图| 亚洲精品色婷婷福利天堂| 亚洲第一中文字幕| 国产亚洲精品久久久久婷婷瑜伽| 国产精品第一区| 欧美日韩国产精品自在自线| 欧美激情一区二区三级高清视频| 欧美www视频| 欧美精品精品一区| 欧美日韩国产综合视频在线| 欧美喷潮久久久xxxxx| 欧美精品三级| 欧美激情一区二区三区全黄| 欧美—级a级欧美特级ar全黄| 欧美高清日韩| 欧美日韩一区二区三区在线看| 欧美日韩免费一区二区三区| 欧美午夜精品久久久久久人妖| 欧美性大战久久久久久久蜜臀| 国产精品wwwwww| 国产日韩欧美综合在线| 狠狠综合久久av一区二区小说| 在线观看国产精品网站| 91久久久亚洲精品| 一区二区三区日韩在线观看| 亚洲视频第一页| 欧美在线首页| 欧美成人精精品一区二区频| 亚洲国产美女| 亚洲一区3d动漫同人无遮挡| 午夜在线不卡| 美女成人午夜| 国产精品久久久久aaaa樱花| 国产亚洲精品自拍| 亚洲美女在线国产| 欧美亚洲三区| 欧美激情按摩在线| 夜久久久久久| 久久久青草青青国产亚洲免观| 欧美高清不卡| 国产午夜精品全部视频在线播放| 亚洲第一毛片| 亚洲欧美国产三级| 蜜臀久久99精品久久久画质超高清 | 一区二区三区欧美| 久久久亚洲高清| 欧美午夜激情在线| 一区福利视频| 亚洲女爱视频在线| 欧美激情视频免费观看| 亚洲亚洲精品在线观看| 免费影视亚洲| 国产一区二区日韩精品| 这里只有精品丝袜| 免费成人美女女| 亚洲午夜羞羞片| 欧美激情精品久久久久久| 国产香蕉久久精品综合网| 日韩系列在线| 欧美va亚洲va国产综合| 亚洲免费在线播放| 欧美另类视频在线| 亚洲风情在线资源站| 欧美一区二区三区精品 | 亚洲在线视频一区| 欧美日韩国产成人高清视频| 黄色成人91| 欧美怡红院视频一区二区三区| 亚洲激情在线| 欧美.com| 在线欧美亚洲| 久久综合久久综合这里只有精品 | 99国产精品| 欧美女同视频| 亚洲精品国久久99热| 久久久xxx| 欧美一进一出视频| 国产精品任我爽爆在线播放| 一个色综合av| 亚洲乱码视频| 欧美精品福利视频| 久久全国免费视频| 国产综合av| 久久久久久一区二区| 欧美一区二区成人| 国产亚洲视频在线| 久久精品国产久精国产思思| 亚洲校园激情| 国产精品午夜春色av| 午夜在线视频一区二区区别| 一区二区三区四区五区视频| 欧美精品在线观看91| 一级日韩一区在线观看| 亚洲毛片在线看| 欧美连裤袜在线视频| 正在播放亚洲| 野花国产精品入口| 国产精品免费看片| 欧美在线视频一区二区| 性一交一乱一区二区洋洋av| 国产亚洲精品v| 毛片精品免费在线观看| 欧美成人日韩| 亚洲先锋成人| 羞羞答答国产精品www一本 | 暖暖成人免费视频|