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

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>
            蜜桃久久av| 久久乐国产精品| 亚洲精华国产欧美| 欧美二区在线看| 日韩一本二本av| 一区二区三区 在线观看视| 欧美日韩国产成人在线免费| 99re66热这里只有精品4| 亚洲精品资源美女情侣酒店| 欧美日韩三级在线| 亚洲欧美精品中文字幕在线| 亚洲欧美经典视频| 永久域名在线精品| 亚洲高清视频在线| 欧美午夜片在线免费观看| 午夜欧美大尺度福利影院在线看| 亚洲免费婷婷| 在线成人欧美| 99精品久久久| 韩国三级在线一区| 亚洲精品日韩精品| 国产日韩精品入口| 亚洲国产一区二区三区高清 | 久久综合激情| 欧美激情第六页| 欧美一区二区| 能在线观看的日韩av| 亚洲线精品一区二区三区八戒| 午夜免费日韩视频| 亚洲日本欧美天堂| 午夜精品美女自拍福到在线 | 欧美日韩一区综合| 久久精品综合一区| 欧美日本韩国| 久久人人爽人人爽| 欧美丝袜一区二区| 噜噜噜久久亚洲精品国产品小说| 欧美日韩另类字幕中文| 久久久久成人精品免费播放动漫| 欧美理论视频| 美女图片一区二区| 国产九色精品成人porny| 亚洲七七久久综合桃花剧情介绍| 国产欧美精品国产国产专区| 亚洲精品一二三| 激情小说另类小说亚洲欧美 | 欧美激情片在线观看| 国产精品美腿一区在线看| 欧美高清视频在线| 狠狠色综合网| 亚洲欧美综合v| 午夜日韩在线观看| 欧美日韩一区二区视频在线观看| 欧美国产视频在线| 黑人一区二区三区四区五区| 中文一区在线| 亚洲淫片在线视频| 欧美精品成人在线| 91久久精品www人人做人人爽| 黄色成人av在线| 久久激情视频免费观看| 久久激情综合网| 国产嫩草一区二区三区在线观看 | 在线亚洲一区观看| 亚洲视频精品| 欧美日韩中文字幕在线| 亚洲精品一区二区三区四区高清| 亚洲国产精品一区二区第一页 | 亚洲中午字幕| 亚洲欧美日韩国产一区| 国产精品国色综合久久| 在线亚洲一区| 欧美一区日韩一区| 国产视频在线观看一区二区| 亚洲欧美国产77777| 欧美一区二区三区四区夜夜大片| 国产精品美女主播| 欧美伊人影院| 裸体一区二区三区| 亚洲国产视频直播| 欧美国产日韩一区二区| 亚洲精品一区二区三区不| 亚洲午夜久久久久久久久电影网| 欧美色图五月天| 亚洲一区二区免费在线| 久久精品中文字幕一区| 一区精品在线播放| 欧美黄色日本| 亚洲综合色激情五月| 久久午夜激情| 亚洲精品中文字幕有码专区| 欧美色网在线| 欧美在线观看视频在线| 欧美黄色免费网站| 亚洲影院色无极综合| 国产午夜精品理论片a级探花| 久久久欧美精品sm网站| 亚洲高清在线精品| 亚洲欧美日韩一区二区| 国内精品久久久久伊人av| 欧美国产极速在线| 亚洲一区二区三区在线视频| 麻豆精品传媒视频| 亚洲精品乱码久久久久| 国产精品永久免费观看| 久热re这里精品视频在线6| 一区二区福利| 每日更新成人在线视频| 亚洲一级高清| 在线成人h网| 国产精品久久久久久久久借妻 | 亚洲国产va精品久久久不卡综合| 99re热这里只有精品视频| 国产老肥熟一区二区三区| 欧美国产欧美亚洲国产日韩mv天天看完整 | 久久久久久久999精品视频| 亚洲精品久久久久久久久久久久久| 欧美日韩午夜在线视频| 久久女同互慰一区二区三区| 99re在线精品| 欧美高清视频一区二区三区在线观看| 午夜国产精品影院在线观看 | 国产原创一区二区| 欧美日本一区二区视频在线观看| 欧美在线观看视频| 一区二区欧美视频| 亚洲激情婷婷| 欧美福利视频网站| 久久婷婷色综合| 亚洲欧美国产毛片在线| 一区二区三区av| 亚洲品质自拍| 亚洲国产成人av在线| 国产亚洲一区二区在线观看| 国产精品久久久久三级| 欧美日韩18| 欧美精品亚洲二区| 欧美成人午夜剧场免费观看| 欧美一级久久久| 亚洲欧美视频一区| 亚洲综合成人在线| 亚洲一区二区三区国产| 亚洲特黄一级片| 亚洲手机视频| 亚洲午夜成aⅴ人片| 一区二区高清在线| 亚洲调教视频在线观看| 亚洲网站视频| 欧美亚洲视频| 久久精品欧美日韩| 久久免费国产精品| 毛片av中文字幕一区二区| 久久综合电影| 欧美激情视频在线播放| 欧美激情偷拍| 国产精品不卡在线| 国产精品你懂得| 国产视频一区免费看| 禁断一区二区三区在线| 在线不卡中文字幕| 日韩午夜视频在线观看| 亚洲视频在线观看三级| 欧美一区1区三区3区公司| 久久久久国产精品人| 免费欧美在线| 亚洲国产免费| 一本色道久久加勒比88综合| 亚洲在线免费| 麻豆精品精华液| 欧美另类在线观看| 国产精品一区二区三区乱码| 国产自产在线视频一区| 亚洲国产天堂久久综合网| 日韩亚洲视频| 久久久久久久尹人综合网亚洲| 欧美大片免费看| 一本大道久久a久久精二百| 午夜精品久久久| 牛牛国产精品| 国产精品资源| 亚洲毛片av| 久久久在线视频| 日韩午夜精品视频| 久久精品国产亚洲a| 欧美日韩国产色视频| 国内精品久久久久久| 一区二区三区四区五区精品| 久久国内精品视频| 日韩午夜在线| 另类春色校园亚洲| 国产精品综合不卡av| 亚洲欧洲日韩女同| 久久国产精品第一页| 91久久久久久| 久久精品中文| 国产女精品视频网站免费| 日韩亚洲欧美成人| 欧美aⅴ99久久黑人专区| 亚洲综合第一| 欧美日韩一区二区三区高清|