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

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://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.
所以成員初始化不是一股腦兒都放到初始化列表里才是最優方案!

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

<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>
            欧美 日韩 国产一区二区在线视频| 亚洲欧美国产一区二区三区| 免费不卡视频| 女生裸体视频一区二区三区| 亚洲另类视频| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 欧美三级午夜理伦三级中视频| 一本久道久久久| 亚洲综合欧美| 影音先锋欧美精品| 亚洲美女av网站| 国产欧美一区二区三区沐欲| 欧美成人免费网站| 欧美日韩亚洲在线| 久久久精品999| 欧美日本亚洲| 久久精品日产第一区二区| 久久乐国产精品| 亚洲一级免费视频| 久久婷婷久久| 亚洲欧美日韩在线| 免费成年人欧美视频| 欧美在线一级视频| 一区二区不卡在线视频 午夜欧美不卡'| 夜夜爽夜夜爽精品视频| 黄色综合网站| 一区二区三区偷拍| 最近看过的日韩成人| 亚洲在线观看视频网站| 亚洲人体1000| 久久精品国产精品亚洲精品| 亚洲一区二区三区欧美| 久久婷婷av| 久久九九热re6这里有精品| 欧美精品久久久久久久免费观看 | 亚洲国产精品一区二区第四页av| 亚洲少妇自拍| 日韩午夜视频在线观看| 久久另类ts人妖一区二区| 亚洲一区日韩| 欧美日韩国产精品一区二区亚洲| 久久在线免费观看| 国产欧美另类| 亚洲一区3d动漫同人无遮挡| 艳妇臀荡乳欲伦亚洲一区| 久久伊人免费视频| 久久综合给合| 国产亚洲va综合人人澡精品| 亚洲一区二三| 亚洲一区在线视频| 欧美丝袜一区二区| 一区二区三区国产| 亚洲午夜一区| 欧美三级欧美一级| 一片黄亚洲嫩模| 一卡二卡3卡四卡高清精品视频| 免费一区视频| 亚洲福利免费| 亚洲激情六月丁香| 欧美成人性网| 亚洲国产免费| 夜夜狂射影院欧美极品| 欧美精品一区二区蜜臀亚洲| 亚洲精品久久久久| 中文在线不卡视频| 国产精品vip| 亚洲欧美国产制服动漫| 欧美一区午夜精品| 国产一区二区在线免费观看 | 欧美国产欧美亚洲国产日韩mv天天看完整 | 久久亚洲风情| 在线免费日韩片| 免费成人毛片| 亚洲日本黄色| 99在线热播精品免费| 欧美日韩美女一区二区| 亚洲一区二区三区国产| 久久精品99国产精品酒店日本| 国产在线观看精品一区二区三区| 久久久精彩视频| 亚洲欧洲精品一区二区三区波多野1战4| 亚洲精品日韩激情在线电影| 欧美精品一区二区三区视频| 在线综合亚洲| 美女日韩欧美| 亚洲视频碰碰| 国语精品中文字幕| 欧美大片在线看| 亚洲深夜影院| 欧美ed2k| 欧美一级视频精品观看| 亚洲二区精品| 国产精品青草久久| 久久亚洲电影| 这里只有精品丝袜| 老色鬼久久亚洲一区二区| 一区二区三区高清在线观看| 国产欧美一区二区精品忘忧草 | 久久精品30| 亚洲剧情一区二区| 久久久综合视频| 亚洲视频电影在线| 尤物九九久久国产精品的分类| 欧美激情一区二区三区在线视频观看| 在线中文字幕不卡| 亚洲电影免费在线观看| 性色av一区二区三区红粉影视| 亚洲承认在线| 国产精品自拍三区| 欧美日韩大片| 久久综合九色综合欧美就去吻| 中日韩午夜理伦电影免费| 欧美激情一区二区三区蜜桃视频 | 亚洲精品永久免费| 黄色国产精品| 国产精品毛片在线| 欧美高清一区| 久久午夜电影网| 欧美有码在线观看视频| 亚洲深夜激情| 99re在线精品| 亚洲黄一区二区三区| 鲁大师成人一区二区三区| 性做久久久久久久久| 中文日韩在线视频| 一本色道久久加勒比88综合| 亚洲第一天堂av| 影音欧美亚洲| 在线看欧美视频| 黄网站色欧美视频| 狠狠色综合网| 狠狠色丁香久久婷婷综合丁香 | 国产精品卡一卡二卡三| 欧美日韩高清在线观看| 欧美久久电影| 欧美激情第1页| 欧美麻豆久久久久久中文| 免费国产自线拍一欧美视频| 毛片一区二区三区| 狂野欧美激情性xxxx欧美| 久久日韩粉嫩一区二区三区| 久久精品国内一区二区三区| 久久精品人人做人人爽电影蜜月| 欧美一区二区私人影院日本| 午夜视频久久久久久| 午夜精品国产精品大乳美女| 午夜精品久久久久久久蜜桃app| 亚洲一区在线直播| 欧美尤物巨大精品爽| 久久久久免费| 欧美激情小视频| 国产精品二区二区三区| 国产精品一区一区| 国产在线观看一区| 亚洲福利视频免费观看| 日韩性生活视频| 亚洲欧美日韩中文视频| 久久精品视频在线| 欧美电影免费观看网站| 亚洲精品国产精品乱码不99 | 欧美激情一区二区三区在线视频观看 | 1024日韩| 中国成人在线视频| 欧美在线影院在线视频| 免费观看欧美在线视频的网站| 欧美激情精品久久久久久变态| 亚洲精品影院| 欧美一区二区三区在线播放| 免费欧美高清视频| 国产精品免费aⅴ片在线观看| 国产亚洲人成a一在线v站| 亚洲国产成人高清精品| 亚洲视频在线观看三级| 久久久久五月天| 亚洲精品一区二区在线观看| 欧美在线视频免费播放| 欧美精品成人一区二区在线观看| 国产精品影视天天线| 亚洲欧洲综合另类| 欧美一区二粉嫩精品国产一线天| 欧美大片免费| 午夜在线a亚洲v天堂网2018| 欧美freesex交免费视频| 国产精品天美传媒入口| 亚洲人www| 久久久99精品免费观看不卡| 亚洲精品欧洲精品| 久久久久一区二区三区| 国产精品专区一| 一本色道久久综合亚洲精品小说 | 日韩写真视频在线观看| 久久午夜av| 狠狠爱www人成狠狠爱综合网| 亚洲视频在线观看| 亚洲国产天堂久久综合| 久久久亚洲成人| 国内精品美女在线观看| 欧美一区二区成人| 亚洲网站在线看| 欧美日韩视频第一区|