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

tommy

It's hard to tell the world we live in is either a reality or a dream
posts - 52, comments - 17, trackbacks - 0, articles - 0
  C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

簡(jiǎn)而言之:explicit修飾的構(gòu)造函數(shù)不能擔(dān)任轉(zhuǎn)換函數(shù)

這個(gè) 《ANSI/ISO C++ Professional Programmer's Handbook 》是這樣說的

explicit Constructors
A constructor that takes a single argument is, by default, an implicit conversion operator, which converts its argument to
an object of its class (see also Chapter 3, "Operator Overloading"). Examine the following concrete example:
class string
{
private:
int size;
int capacity;
char *buff;
public:
string();
string(int size); // constructor and implicit conversion operator
string(const char *); // constructor and implicit conversion operator
~string();
};
Class string has three constructors: a default constructor, a constructor that takes int, and a constructor that
constructs a string from const char *. The second constructor is used to create an empty string object with an
initial preallocated buffer at the specified size. However, in the case of class string, the automatic conversion is
dubious. Converting an int into a string object doesn't make sense, although this is exactly what this constructor does.

Consider the following:
int main()
{
string s = "hello"; //OK, convert a C-string into a string object
int ns = 0;
s = 1; // 1 oops, programmer intended to write ns = 1,
}
In the expression s= 1;, the programmer simply mistyped the name of the variable ns, typing s instead. Normally,
the compiler detects the incompatible types and issues an error message. However, before ruling it out, the compiler first
searches for a user-defined conversion that allows this expression; indeed, it finds the constructor that takes int.
Consequently, the compiler interprets the expression s= 1; as if the programmer had written
s = string(1);
You might encounter a similar problem when calling a function that takes a string argument. The following example
can either be a cryptic coding style or simply a programmer's typographical error. However, due to the implicit
conversion constructor of class string, it will pass unnoticed:
int f(string s);
int main()
{
f(1); // without a an explicit constructor,
//this call is expanded into: f ( string(1) );
//was that intentional or merely a programmer's typo?
}
'In order to avoid such implicit conversions, a constructor that takes one argument needs to be declared explicit:
class string
{
//...
public:
explicit string(int size); // block implicit conversion
string(const char *); //implicit conversion
~string();
};
An explicit constructor does not behave as an implicit conversion operator, which enables the compiler to catch the
typographical error this time:
int main()
{
string s = "hello"; //OK, convert a C-string into a string object
int ns = 0;
s = 1; // compile time error ; this time the compiler catches the typo
}
Why aren't all constructors automatically declared explicit? Under some conditions, the automatic type conversion is
useful and well behaved. A good example of this is the third constructor of string:
string(const char *);

The implicit type conversion of const char * to a string object enables its users to write the following:
string s;
s = "Hello";
The compiler implicitly transforms this into
string s;
//pseudo C++ code:
s = string ("Hello"); //create a temporary and assign it to s
On the other hand, if you declare this constructor explicit, you have to use explicit type conversion:
class string
{
//...
public:
explicit string(const char *);
};
int main()
{
string s;
s = string("Hello"); //explicit conversion now required
return 0;
}
Extensive amounts of legacy C++ code rely on the implicit conversion of constructors. The C++ Standardization
committee was aware of that. In order to not make existing code break, the implicit conversion was retained. However, a
new keyword, explicit, was introduced to the languageto enable the programmer to block the implicit conversion
when it is undesirable. As a rule, a constructor that can be invoked with a single argument needs to be declared
explicit. When the implicit type conversion is intentional and well behaved, the constructor can be used as an
implicit conversion operator.

Feedback

# re: 關(guān)于C++的 explicit關(guān)鍵字  回復(fù)  更多評(píng)論   

2006-04-18 10:45 by youtr
呵呵,寫的不錯(cuò)

# re: 關(guān)于C++的 explicit關(guān)鍵字  回復(fù)  更多評(píng)論   

2006-09-07 17:11 by 11
In the expression s= 1;, the programmer simply mistyped the name of the variable ns, typing s instead. Normally,
the compiler detects the incompatible types and issues an error message. However, before ruling it out, the compiler first
searches for a user-defined conversion that allows this expression; indeed, it finds the constructor that takes int.
Consequently, the compiler interprets the expression s= 1; as if the programmer had written

# re: 關(guān)于C++的 explicit關(guān)鍵字  回復(fù)  更多評(píng)論   

2008-06-20 11:13 by Anecdote
學(xué)習(xí)了。謝謝

# re: 關(guān)于C++的 explicit關(guān)鍵字[未登錄]  回復(fù)  更多評(píng)論   

2009-02-18 15:23 by Lisa
講的很透徹!

# re: 關(guān)于C++的 explicit關(guān)鍵字  回復(fù)  更多評(píng)論   

2009-03-16 10:35 by inzer
不錯(cuò)

# re: 關(guān)于C++的 explicit關(guān)鍵字  回復(fù)  更多評(píng)論   

2009-04-29 11:00 by 一樣一樣
透徹,明白了
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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ⅴ国产紧身牛仔裤| 欧美在线电影| 麻豆成人精品| 欧美日韩卡一卡二| 国产精品日韩在线观看| 国产一区二区三区直播精品电影| 国产亚洲欧美日韩一区二区| 樱花yy私人影院亚洲| 亚洲精品乱码久久久久久黑人 | 亚洲午夜一级| 午夜国产不卡在线观看视频| 久久精品亚洲热| 欧美精品1区| 国产精品久久国产精麻豆99网站| 国产精品久久久爽爽爽麻豆色哟哟| 国产精品毛片| 国产一区欧美| 日韩亚洲综合在线| 久久成年人视频| 老牛影视一区二区三区| 亚洲国产另类久久精品| 亚洲国产精品黑人久久久| 99国产精品国产精品毛片| 香蕉久久夜色| 欧美精品久久99| 国产欧美精品国产国产专区| 最新中文字幕一区二区三区| 欧美一区亚洲二区| 亚洲欧洲精品一区二区三区波多野1战4 | 亚洲二区三区四区| 夜色激情一区二区| 久久综合网hezyo| 亚洲美女精品成人在线视频| 午夜精品美女久久久久av福利| 久久婷婷成人综合色| 亚洲精品在线观看免费| 久久久噜噜噜| 久久久噜噜噜久久中文字免| 欧美日韩中国免费专区在线看| 红桃视频国产一区| 性色av香蕉一区二区| 亚洲人久久久| 久久综合久久综合久久综合| 国产日韩欧美中文| 亚洲在线成人精品| 亚洲人被黑人高潮完整版| 久久久久久69| 国产欧美一区二区在线观看| 99亚洲视频| 欧美激情无毛| 久久综合伊人77777麻豆| 国产欧美一区二区三区在线老狼| 亚洲一区二区三区免费观看 | 国产精品福利在线观看网址| 亚洲电影观看| 久久久久久一区| 亚洲欧美日韩人成在线播放| 欧美日韩综合在线免费观看| 亚洲国产欧美在线人成| 久久伊人精品天天| 午夜在线不卡| 国产欧美日韩亚洲精品| 午夜精品亚洲| 亚洲男人的天堂在线| 国产精品国产三级国产aⅴ入口| 这里只有精品视频| 日韩视频第一页| 欧美日韩亚洲91| 亚洲一区二区三区精品动漫| 午夜宅男久久久| 久久免费视频在线观看| 欧美黄色小视频| 国产精品99久久不卡二区| 欧美日韩免费高清一区色橹橹| 亚洲精品极品| 亚洲日本欧美日韩高观看| 欧美精品一区二区蜜臀亚洲| 一区二区欧美在线| 一区二区三区视频免费在线观看 | 欧美日韩精品免费| 亚洲综合电影一区二区三区| 亚洲女人天堂av| 国产亚洲福利社区一区| 久久综合色8888| 牛牛国产精品| 亚洲一区欧美激情| 欧美一区二区视频在线观看2020| 国产有码一区二区| 亚洲国产精品尤物yw在线观看| 欧美日韩国产三级| 久久精品成人一区二区三区蜜臀| 久久野战av| 亚洲欧美国产制服动漫| 欧美在线观看一二区| 最近看过的日韩成人| 一区二区三区欧美在线观看| 狠狠色狠狠色综合日日tαg| 亚洲成人在线网| 国产精品国产三级国产普通话99| 久久婷婷麻豆| 欧美视频专区一二在线观看| 久久综合国产精品台湾中文娱乐网| 欧美bbbxxxxx| 欧美影院午夜播放| 欧美精品色综合| 欧美在线日韩精品| 欧美激情视频一区二区三区在线播放| 午夜视频久久久| 蜜臀91精品一区二区三区| 亚洲视频电影图片偷拍一区| 久久久噜噜噜久久中文字免| 亚洲欧美日本国产有色| 欧美成年人视频网站| 欧美亚洲综合另类| 欧美另类综合| 欧美成人精品高清在线播放| 国产精品黄色| 亚洲激情精品| 欧美韩日一区| 欧美大胆a视频| 国产主播精品在线| 亚洲欧美国产va在线影院| 在线视频你懂得一区二区三区| 久久精品在线| 久久久久亚洲综合| 国产视频在线一区二区| 这里只有精品视频| 一区二区欧美国产| 欧美激情精品久久久久久蜜臀| 久久婷婷成人综合色| 国产女主播一区| 亚洲图片在线观看| 亚洲欧美日韩精品久久| 欧美日韩国产首页在线观看| 亚洲一区二区三区777| 欧美日韩在线亚洲一区蜜芽| 亚洲精品在线三区| 一本久久综合亚洲鲁鲁五月天| 欧美成年人视频网站欧美| 欧美成人乱码一区二区三区| 极品中文字幕一区| 久久亚洲免费| 亚洲国产成人不卡| 亚洲精品影视在线观看| 米奇777在线欧美播放| 亚洲国产成人av在线| 国产精品高清网站| 亚洲一区二区免费看| 午夜精品久久久久久| 国产精品视频第一区| 亚洲综合99| 久久人91精品久久久久久不卡| 一区在线观看| avtt综合网| 欧美一区二区视频免费观看| 国产精品色婷婷| 久久se精品一区精品二区| 美女久久一区| 亚洲精品一区二区三区在线观看 | 亚洲激情在线视频| 一本不卡影院| 国产精品久久久久av| 亚洲欧美国内爽妇网| 巨乳诱惑日韩免费av| 亚洲另类黄色| 国产精品v欧美精品∨日韩| 性感少妇一区| 亚洲人成毛片在线播放| 亚洲欧美日本精品| 亚洲国产精品久久久久婷婷老年 | 久久久亚洲国产天美传媒修理工 | 国产精品久久国产精品99gif| 亚洲欧洲av一区二区三区久久| 免费观看30秒视频久久| 欧美阿v一级看视频| 中文精品视频一区二区在线观看| 久久99伊人| 亚洲卡通欧美制服中文| 国产精品一级| 久久精品最新地址| 99国产麻豆精品| 久久综合色综合88| 亚洲与欧洲av电影| 亚洲黄色性网站| 国产欧美日韩免费| 欧美连裤袜在线视频| 久久久精彩视频| av成人天堂| 欧美激情一区二区三区在线视频| 亚洲欧美日韩一区二区三区在线观看| 在线成人性视频| 国产欧美日韩综合精品二区| 欧美精品激情在线观看| 久久久久久久久久久久久久一区 | 亚洲精品国产无天堂网2021| 国产精品美女久久久浪潮软件|