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

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

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


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

作者: Jerry Cat
時間: 2006/05/12
鏈接:?
http://www.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.
所以成員初始化不是一股腦兒都放到初始化列表里才是最優(yōu)方案!

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. 還是那句話: 別將所有的成員初始化工作全放在構(gòu)造函數(shù)的初始化列表里 -
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. 成員初始化列表的內(nèi)容"插"在構(gòu)造函數(shù)的最前端.

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

只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理



<2006年7月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用鏈接

留言簿(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>
            国产亚洲精品一区二区| 久久黄色网页| 欧美精品一卡二卡| 亚洲第一在线| 久热re这里精品视频在线6| 亚洲综合色婷婷| 国产精品羞羞答答| 亚洲在线不卡| 国产精品主播| 久久成人资源| 欧美国产精品劲爆| 午夜精品福利一区二区三区av| 欧美视频在线免费看| 亚洲女ⅴideoshd黑人| 欧美中在线观看| 国模套图日韩精品一区二区| 欧美成年网站| 欧美体内she精视频| 久久手机免费观看| 欧美精品自拍| 久久久一二三| 欧美视频网址| 欧美ab在线视频| 国产精品日韩久久久久| 欧美a级理论片| 国产精品日日摸夜夜添夜夜av| 亚洲国产一区在线观看| 在线综合亚洲欧美在线视频| 国产专区精品视频| 99日韩精品| 亚洲片在线观看| 欧美一区二区视频在线观看2020| 嫩模写真一区二区三区三州| 亚洲综合精品| 欧美精品在线观看91| 欧美在线亚洲| 欧美系列电影免费观看| 欧美99久久| 国产亚洲激情| 亚洲自啪免费| 亚洲一卡久久| 欧美巨乳在线| 亚洲激情成人网| 国产在线精品成人一区二区三区 | 欧美一区二区三区四区视频| 伊人久久大香线| 香蕉久久一区二区不卡无毒影院| 国产精品magnet| 亚洲欧洲日本一区二区三区| 在线播放不卡| 欧美在线观看视频| 亚洲欧美日本伦理| 国产精品成人久久久久| 91久久精品美女高潮| 亚洲成人直播| 蜜臀久久99精品久久久画质超高清| 精品福利电影| 久久不见久久见免费视频1| 亚洲欧美欧美一区二区三区| 欧美激情91| 亚洲毛片在线免费观看| 亚洲毛片在线看| 欧美另类69精品久久久久9999| 亚洲一区二区三区在线观看视频| 9久re热视频在线精品| 亚洲国产小视频| 蜜桃av噜噜一区| 亚洲高清色综合| 99re视频这里只有精品| 欧美精品电影| 一本色道久久综合亚洲91| 亚洲午夜国产一区99re久久| 欧美少妇一区二区| 亚洲综合国产激情另类一区| 欧美在线观看一区| 一色屋精品视频免费看| 久色成人在线| 91久久精品www人人做人人爽| 国产精品一区二区欧美| 亚洲欧美影院| 免费高清在线一区| 亚洲欧洲另类| 欧美性理论片在线观看片免费| 欧美在线观看一区二区| 国内自拍一区| 欧美不卡三区| 亚洲一卡二卡三卡四卡五卡| 亚洲欧美日韩国产综合在线| 国产精品午夜电影| 久久久亚洲综合| 日韩视频在线你懂得| 欧美中文在线观看| 亚洲国产一区视频| 国产精品欧美经典| 久久综合伊人77777| 一区二区三区久久网| 久久亚洲视频| 亚洲狼人精品一区二区三区| 国产精品毛片a∨一区二区三区|国 | 欧美大片专区| 中日韩美女免费视频网站在线观看| 一区二区三区中文在线观看 | 亚洲一区在线观看视频 | 性欧美xxxx大乳国产app| 狠狠v欧美v日韩v亚洲ⅴ| 欧美黄在线观看| 久久精品动漫| 在线一区二区三区四区五区| 久久综合九色综合欧美就去吻| 国产一区二区三区成人欧美日韩在线观看| 亚洲高清资源| 久久不射2019中文字幕| 夜夜嗨av一区二区三区四季av| 久久夜精品va视频免费观看| av成人动漫| 91久久久久久| 欧美bbbxxxxx| 久久久蜜桃一区二区人| 亚洲综合色在线| 一区二区精品在线| 亚洲大胆美女视频| 国产一区二区三区日韩| 国产精品黄视频| 欧美日韩国产一区精品一区 | 在线亚洲一区二区| 亚洲高清影视| 欧美不卡视频一区发布| 欧美亚洲视频一区二区| 亚洲图片在区色| 亚洲婷婷国产精品电影人久久| 国产精品a级| 欧美日韩理论| 欧美日韩精品免费观看视一区二区 | 欧美一区网站| 亚洲欧美日韩国产综合在线| 亚洲视频欧美在线| 亚洲精品五月天| 亚洲精品乱码| 日韩亚洲欧美一区| 亚洲久色影视| 一区二区激情视频| 亚洲视频一二区| 亚洲私人影吧| 亚洲综合电影| 久久av资源网站| 久久免费偷拍视频| 欧美bbbxxxxx| 亚洲精品国产精品国自产在线| 欧美一区二区三区日韩| 欧美一区二区三区另类| 欧美一级精品大片| 久久狠狠久久综合桃花| 久久午夜色播影院免费高清| 久久久噜噜噜久久久| 玖玖在线精品| 亚洲激情另类| 亚洲视频精品| 午夜国产欧美理论在线播放| 午夜精品久久久久久久蜜桃app| 欧美二区在线| 亚洲精品中文字幕在线| 99视频在线精品国自产拍免费观看| 久久精品日产第一区二区三区 | 国产精品青草综合久久久久99| 久久精品视频在线看| 卡一卡二国产精品| 欧美视频官网| 激情久久一区| 中文在线不卡视频| 狂野欧美激情性xxxx| 亚洲欧洲在线一区| 亚洲免费视频观看| 久久一本综合频道| 国产精品爱啪在线线免费观看 | 亚洲精品国产日韩| 性欧美精品高清| 久久久亚洲影院你懂的| 欧美系列一区| 亚洲盗摄视频| 久久久成人精品| 99在线热播精品免费| 久久九九久久九九| 国产精品国产三级国产aⅴ浪潮| 欧美精品情趣视频| 国产在线日韩| 这里只有视频精品| 美女主播精品视频一二三四| 日韩视频免费在线观看| 久久久五月天| 国产麻豆午夜三级精品| 99综合精品| 欧美成在线观看| 久久精品国产欧美亚洲人人爽| 午夜精品久久久久久久久| 久久综合福利| 狠久久av成人天堂| 久久精品国产久精国产一老狼| 久久不射网站| 一区二区三区精品视频| 另类尿喷潮videofree|