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

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://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.
所以成員初始化不是一股腦兒都放到初始化列表里才是最優(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>
            亚洲裸体在线观看| 亚洲五月六月| 女女同性精品视频| 久久天天躁狠狠躁夜夜av| 激情伊人五月天久久综合| 老司机成人在线视频| 美女任你摸久久| 一本色道久久综合狠狠躁篇的优点| 亚洲精品一二三| 欧美视频一区二区三区在线观看 | 国产亚洲欧美色| 欧美a级一区二区| 欧美精品久久久久久久免费观看 | 日韩亚洲精品电影| 国产亚洲午夜| 亚洲国产精品热久久| 欧美三级在线| 久久―日本道色综合久久| 免费观看成人鲁鲁鲁鲁鲁视频 | 国产精品成人免费视频| 久久精品国产精品亚洲精品| 久久午夜视频| 亚洲免费婷婷| 久久亚洲精品一区二区| 亚洲午夜激情免费视频| 久久久久久自在自线| 一区二区三区不卡视频在线观看| 国内久久视频| 亚洲精品视频免费观看| 国产一区视频在线看| 91久久在线观看| 国产精品午夜电影| 亚洲高清在线| 国产精品天美传媒入口| 欧美激情一区二区在线| 国产亚洲人成a一在线v站| 亚洲黄一区二区三区| 韩国一区二区在线观看| 一区二区三区日韩欧美| 91久久精品日日躁夜夜躁欧美| 亚洲午夜女主播在线直播| 亚洲人妖在线| 久久亚洲精品欧美| 久久精品国产v日韩v亚洲 | 国产精品欧美经典| 91久久精品美女| 精品动漫3d一区二区三区| 亚洲男人的天堂在线| 亚洲一区视频在线观看视频| 欧美大胆成人| 欧美国产日韩一区二区在线观看| 国产在线欧美日韩| 亚洲欧美日韩一区二区三区在线| 宅男精品视频| 欧美日本在线看| 亚洲国产精品高清久久久| 在线观看不卡| 玖玖玖免费嫩草在线影院一区| 久久精品一区二区国产| 国产一区二区久久精品| 新67194成人永久网站| 欧美在线观看一区二区| 国产农村妇女精品一二区| 亚洲免费中文| 久久久亚洲影院你懂的| 韩国v欧美v日本v亚洲v| 久久精品一区蜜桃臀影院| 久久一本综合频道| 在线观看的日韩av| 媚黑女一区二区| 亚洲国产精品成人久久综合一区| 91久久国产综合久久蜜月精品 | 美女图片一区二区| 亚洲国产女人aaa毛片在线| 亚洲三级网站| 亚洲人成在线观看一区二区 | 国模精品娜娜一二三区| 久久天堂精品| 亚洲靠逼com| 亚洲免费视频在线观看| 国产亚洲欧美另类中文| 久久午夜激情| 99国产精品视频免费观看| 亚洲欧美日韩一区| 国语对白精品一区二区| 欧美夫妇交换俱乐部在线观看| 亚洲精品国产精品国自产观看浪潮| 一区二区三区精密机械公司 | 国产精品网站在线| 久久精品官网| 亚洲精品一区二区三区不| 欧美一区二区三区在| 激情五月***国产精品| 欧美精品手机在线| 香蕉尹人综合在线观看| 亚洲国产一区二区精品专区| 亚洲女人天堂成人av在线| 国内成人精品视频| 欧美日韩中文字幕| 欧美综合国产| 在线视频日韩精品| 女生裸体视频一区二区三区| 亚洲天堂偷拍| 在线观看成人av电影| 国产精品观看| 美脚丝袜一区二区三区在线观看 | 欧美激情精品久久久久久变态| 亚洲女女做受ⅹxx高潮| 亚洲精品久久久久久久久| 国产美女精品视频| 欧美日韩国产精品一卡| 久久久午夜精品| 亚洲欧美激情一区| 99re66热这里只有精品3直播| 久久午夜激情| 欧美一级精品大片| 一区二区电影免费观看| 有码中文亚洲精品| 国产人久久人人人人爽| 欧美日精品一区视频| 看欧美日韩国产| 久久riav二区三区| 亚洲欧美日韩爽爽影院| 一区二区三区成人| 亚洲人成网站在线观看播放| 欧美成人综合在线| 巨胸喷奶水www久久久免费动漫| 亚洲欧美精品伊人久久| 一区二区日韩精品| 亚洲三级电影在线观看 | 国产精品v日韩精品| 欧美激情一区二区久久久| 久久亚洲综合色| 久久久久久成人| 久久久久久亚洲精品不卡4k岛国| 亚洲欧美一区在线| 亚洲女女做受ⅹxx高潮| 亚洲一区精品视频| 亚洲欧美日韩人成在线播放| 亚洲一区二区三区四区中文| 国产精品99久久久久久白浆小说 | 久久久激情视频| 久久久久九九九九| 久久亚洲高清| 美女爽到呻吟久久久久| 欧美成人高清视频| 欧美国产先锋| 亚洲人成小说网站色在线| 日韩一级网站| 亚洲自拍偷拍网址| 欧美亚洲免费电影| 久久久.com| 能在线观看的日韩av| 欧美激情精品久久久久久免费印度| 欧美国产日韩a欧美在线观看| 欧美日韩国产黄| 国产精品视频网址| 国产一区二区三区在线观看视频 | 国产综合久久久久久| 在线观看一区| aa亚洲婷婷| 欧美一区二区福利在线| 久久一区二区视频| 亚洲黄色成人网| 亚洲午夜在线观看| 久久精品女人的天堂av| 欧美国产日韩视频| 国产精品一区二区你懂得| 激情校园亚洲| 亚洲神马久久| 久久亚洲精品欧美| 亚洲美女av在线播放| 亚洲欧美日韩精品久久久久| 久久久另类综合| 欧美色一级片| 亚洲第一主播视频| 午夜精品久久久久久久| 免费日韩av片| 亚洲小少妇裸体bbw| 美女视频网站黄色亚洲| 国产乱码精品一区二区三区不卡| 亚洲国产激情| 欧美在线不卡| 日韩一区二区精品视频| 久久九九99视频| 国产精品日日做人人爱| 91久久中文| 久久欧美中文字幕| 亚洲一区观看| 欧美另类变人与禽xxxxx| 很黄很黄激情成人| 亚洲欧美一区二区三区在线| 亚洲高清不卡在线观看| 性欧美大战久久久久久久免费观看| 欧美国产日韩一二三区| 在线播放中文一区| 久久精品国产99| 亚洲尤物精选| 国产精品jvid在线观看蜜臀| 日韩视频在线观看免费|