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

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>
            免费久久99精品国产| 亚洲一区在线免费观看| 久久久久中文| 久久精品亚洲精品国产欧美kt∨| 国产一区二区三区网站| 看片网站欧美日韩| 欧美极品一区| 性欧美18~19sex高清播放| 亚洲免费在线精品一区| 狠狠色噜噜狠狠色综合久| 欧美风情在线观看| 欧美日韩在线另类| 久久精品国产亚洲一区二区| 久久精品国产亚洲a| 亚洲人成在线播放| 亚洲调教视频在线观看| 韩国三级电影一区二区| 亚洲精品中文字幕在线观看| 国产日韩精品久久久| 欧美国产日韩一区| 国产美女搞久久| 欧美韩日精品| 国产精品美女久久久久久久 | 亚洲福利视频网| 日韩一区二区精品视频| 黄色av成人| 99精品国产一区二区青青牛奶| 国产亚洲福利一区| 亚洲人体1000| 狠狠干成人综合网| 亚洲视频一区二区免费在线观看| 伊人久久av导航| 亚洲午夜一级| 一区二区三区视频在线观看 | 亚洲高清一区二| 国产在线精品成人一区二区三区| 亚洲国产综合在线看不卡| 国产精品网红福利| 亚洲精品在线电影| 亚洲精品国产精品国自产观看浪潮 | 精品成人一区| 亚洲欧美综合一区| 亚洲一区二区三区久久| 欧美xxx在线观看| 另类激情亚洲| 国产资源精品在线观看| 亚洲免费视频在线观看| 亚洲午夜在线观看视频在线| 欧美成人中文| 欧美福利电影在线观看| 亚洲毛片av在线| 亚洲国产欧美一区二区三区久久| 亚洲欧美日本另类| 午夜精品在线视频| 国产精品视区| 亚洲欧美亚洲| 久久国产视频网站| 国产麻豆精品theporn| 亚洲天堂成人在线视频| 亚洲在线视频观看| 国产精品美女在线| 亚洲一区在线观看免费观看电影高清 | 午夜精品久久久久久久久| 亚洲欧美资源在线| 国产精品爽黄69| 欧美在线精品一区| 久久天堂国产精品| 在线日韩欧美| 欧美激情精品久久久| 亚洲日本成人女熟在线观看| 亚洲美女啪啪| 国产精品久久久久久av下载红粉 | 在线观看91精品国产入口| 久久深夜福利免费观看| 亚洲第一视频| 亚洲少妇在线| 国产视频在线观看一区二区| 欧美资源在线观看| 欧美激情按摩在线| 亚洲一区二区三区精品视频| 国产精品久久久久久久第一福利| 午夜精品国产| 欧美电影在线播放| 亚洲一区二区免费在线| 国产日韩精品一区二区| 久久久久久亚洲精品中文字幕| 欧美激情视频一区二区三区免费 | 亚洲一区二区伦理| 国产一区日韩二区欧美三区| 麻豆精品国产91久久久久久| 亚洲国产日韩综合一区| 午夜一级在线看亚洲| 1024国产精品| 国产精品女同互慰在线看| 午夜影院日韩| 亚洲激情网站免费观看| 香蕉成人伊视频在线观看| 在线精品一区| 国产精品视频yy9099| 久久青草福利网站| 在线亚洲一区| 亚洲国产日本| 久久久久青草大香线综合精品| 日韩系列在线| 一区在线影院| 国产精品一区视频网站| 欧美精品播放| 久久国产精品久久w女人spa| 99国产精品99久久久久久| 免费国产自线拍一欧美视频| 亚洲男女毛片无遮挡| 亚洲人妖在线| 激情综合在线| 国产精品尤物福利片在线观看| 欧美freesex交免费视频| 欧美亚洲三区| 亚洲午夜久久久久久久久电影网| 欧美69wwwcom| 久久久久久久999精品视频| 亚洲一区二区三区精品在线观看 | 亚洲人精品午夜| 激情小说另类小说亚洲欧美 | 欧美日韩日日夜夜| 欧美成年人网站| 久久午夜精品| 久久久久久亚洲精品中文字幕| 亚欧成人在线| 午夜日韩在线| 午夜激情综合网| 亚洲女人天堂av| 亚洲欧美成人一区二区三区| 99在线|亚洲一区二区| 亚洲久久成人| 亚洲精品欧美日韩专区| 亚洲欧洲精品一区二区精品久久久 | 亚洲一区二区三区乱码aⅴ| 99国产精品久久久久久久久久 | 99re热这里只有精品视频| 91久久久久久久久久久久久| 精品福利电影| 狠狠色综合色综合网络| 国产日韩视频一区二区三区| 国产精品激情偷乱一区二区∴| 欧美日韩国产首页| 欧美日韩国产在线观看| 欧美性猛交一区二区三区精品| 欧美日韩一二区| 国产精品视频不卡| 国产一区二区| 狠狠色丁香婷婷综合影院| 韩国免费一区| 亚洲日本免费电影| 一区二区欧美日韩| 午夜精品在线看| 午夜精品福利一区二区三区av| 久久大综合网| 免费在线日韩av| 亚洲第一精品夜夜躁人人爽| 亚洲精品美女| 亚洲欧美另类国产| 久久久精品性| 欧美日韩久久| 国产私拍一区| 亚洲精品影院| 午夜视频在线观看一区| 另类欧美日韩国产在线| 亚洲国产精品成人一区二区| 一本色道久久综合亚洲精品婷婷| 亚洲欧美精品在线观看| 麻豆国产精品va在线观看不卡| 欧美日韩在线大尺度| 国产日韩在线看| 日韩视频免费观看| 久久国产直播| 亚洲精品美女久久7777777| 亚洲欧美激情诱惑| 免费成人网www| 国产欧美日韩亚洲一区二区三区| 亚洲国产毛片完整版 | 亚洲免费av观看| 欧美一区综合| 欧美好吊妞视频| 国产日本精品| 亚洲网站在线看| 欧美成人免费在线| 亚洲女爱视频在线| 欧美女同视频| 在线观看欧美日韩国产| 欧美一区二区三区免费视| 亚洲大胆人体在线| 欧美自拍丝袜亚洲| 国产精品视频一二三| 亚洲乱亚洲高清| 欧美11—12娇小xxxx| 亚洲欧美999| 欧美视频一区二区三区| 亚洲日韩视频| 亚洲成人自拍视频| 久久久www成人免费精品| 国产免费一区二区三区香蕉精|