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

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 閱讀(885) 評論(0)  編輯 收藏 引用

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理



<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>
            欧美色另类天堂2015| 国产乱理伦片在线观看夜一区| 国内精品久久久久久久影视麻豆| 亚洲一区二区日本| 在线中文字幕不卡| 国产精品亚洲综合一区在线观看| 午夜精品国产| 久久国产精品亚洲va麻豆| 国产亚洲亚洲| 你懂的视频一区二区| 蜜桃av噜噜一区二区三区| 99成人在线| 午夜精品久久久久久久久久久久| 国产午夜精品一区二区三区视频| 久久久久久久久久久一区| 久久婷婷av| 中文av字幕一区| 欧美与黑人午夜性猛交久久久| 在线不卡视频| 夜夜嗨av色一区二区不卡| 国产欧美精品一区二区三区介绍| 久久久久免费| 欧美精品 日韩| 久久精品日韩欧美| 欧美成人自拍| 欧美一区二区大片| 欧美成人亚洲成人日韩成人| 亚洲欧美日韩精品一区二区| 久久免费视频观看| 亚洲永久在线| 久久中文字幕一区| 香蕉久久一区二区不卡无毒影院| 久久精品国产99国产精品| 99av国产精品欲麻豆| 欧美一级日韩一级| 国产精品99久久久久久久久久久久| 午夜久久久久| 亚洲伊人一本大道中文字幕| 久久免费偷拍视频| 欧美一级黄色录像| 欧美日韩一区二区精品| 欧美成年人视频网站| 欧美日韩妖精视频| 欧美国产专区| 黑人巨大精品欧美一区二区小视频| 91久久香蕉国产日韩欧美9色 | 美女精品在线观看| 欧美日韩在线观看一区二区| 欧美成人综合一区| 国产一区观看| 亚洲视频精选在线| 中文av字幕一区| 欧美精品一区三区在线观看| 免费视频一区| 影音先锋在线一区| 欧美在线免费视频| 久久精品99国产精品| 欧美午夜理伦三级在线观看| 亚洲国产精品悠悠久久琪琪| 国产综合久久久久久鬼色| 亚洲在线视频免费观看| 亚洲一级一区| 欧美视频中文字幕在线| 亚洲精品视频在线| 99ri日韩精品视频| 欧美日韩成人综合在线一区二区| 亚洲大黄网站| 亚洲美女精品久久| 欧美~级网站不卡| 亚洲福利视频三区| 日韩亚洲欧美精品| 欧美日本中文字幕| 99精品视频免费全部在线| 国产精品99久久99久久久二8| 欧美日韩精品久久久| 亚洲精品美女| 亚洲欧美精品suv| 国产精品一香蕉国产线看观看| 亚洲一区综合| 可以看av的网站久久看| 伊人狠狠色j香婷婷综合| 久久人人97超碰精品888| 欧美福利精品| 亚洲视频电影图片偷拍一区| 欧美人牲a欧美精品| 中文在线不卡| 欧美一级大片在线观看| 激情欧美一区二区三区| 久久综合九色| 一区二区三区国产在线| 欧美一区二区免费视频| 加勒比av一区二区| 欧美国产精品v| 亚洲一区二区精品在线观看| 久久国产精品久久久久久| 国产在线精品一区二区夜色| 久色成人在线| 亚洲深夜福利| 久久综合九色综合久99| 一区二区三区 在线观看视频| 国产精品视频区| 久久一区二区三区四区五区| 99re在线精品| 久久―日本道色综合久久| 亚洲免费大片| 国产综合色在线| 欧美日韩国产影片| 欧美在线free| 一区二区欧美日韩| 欧美freesex8一10精品| 亚洲在线免费| 亚洲国产成人精品久久| 欧美性猛片xxxx免费看久爱 | 另类尿喷潮videofree| 99国产一区| 国精产品99永久一区一区| 欧美精品激情blacked18| 午夜精品一区二区三区在线| 最新国产の精品合集bt伙计| 久久久人成影片一区二区三区观看 | 欧美肥婆在线| 久久精品国产69国产精品亚洲| 亚洲免费观看高清完整版在线观看熊| 欧美影院在线| 亚洲欧美日韩精品久久亚洲区| 亚洲精品欧美日韩| 国内欧美视频一区二区| 国产精品麻豆成人av电影艾秋 | 在线亚洲国产精品网站| 亚洲国产黄色| 欧美激情精品久久久久久| 久久精品一区二区三区中文字幕| 中文久久精品| 中国成人黄色视屏| 9色国产精品| 99亚洲一区二区| 亚洲人成网站在线播| 亚洲国产天堂网精品网站| 极品少妇一区二区三区精品视频| 国产欧美日韩激情| 国产日韩视频一区二区三区| 国产精品免费在线| 国产精品黄视频| 国产精品久久亚洲7777| 国产精品欧美精品| 国产精品激情av在线播放| 欧美视频日韩视频| 国产精品久久久久77777| 国产精品久久97| 国产精品久久久爽爽爽麻豆色哟哟| 欧美日韩精品免费在线观看视频| 欧美激情精品久久久久久久变态| 欧美xx69| 欧美午夜电影网| 国产日韩欧美综合在线| 国内成人自拍视频| 在线观看中文字幕亚洲| 亚洲人屁股眼子交8| 一本不卡影院| 亚洲欧美一区二区激情| 久久国产视频网站| 开元免费观看欧美电视剧网站| 欧美成人精品1314www| 亚洲高清一区二| 日韩一二三在线视频播| 亚洲综合色丁香婷婷六月图片| 午夜精品久久久久久久99水蜜桃| 欧美在线视频一区二区| 免费一级欧美片在线播放| 欧美日韩精品在线播放| 国产精品入口| 在线看国产日韩| 夜夜嗨av一区二区三区四季av| 亚洲综合色丁香婷婷六月图片| 欧美诱惑福利视频| 欧美激情精品久久久久久免费印度| 亚洲人成在线观看一区二区| 亚洲深夜福利网站| 久久偷看各类wc女厕嘘嘘偷窃| 欧美国产视频日韩| 国产乱码精品一区二区三| 亚洲国产精品久久精品怡红院| 一区二区三区鲁丝不卡| 欧美在线日韩| 亚洲欧洲日本一区二区三区| 午夜精品视频在线观看| 欧美精品 国产精品| 国产偷久久久精品专区| 亚洲精品一区二区三区樱花 | 99视频精品全国免费| 久久精品国产久精国产一老狼| 亚洲国产天堂网精品网站| 亚洲一区二区在线| 欧美国产日韩一区二区在线观看| 国产九色精品成人porny| 亚洲美女尤物影院| 久久久久在线| 亚洲桃色在线一区| 欧美日本在线视频| 亚洲国产高清高潮精品美女|