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

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>
            欧美日韩国产三区| 国产日韩欧美麻豆| 欧美大片在线观看| 国产精品揄拍一区二区| 亚洲欧洲另类| 欧美主播一区二区三区| aa成人免费视频| 欧美成人精品福利| 激情视频一区二区三区| 欧美一区二区三区免费观看视频| 亚洲国产精品va在线看黑人动漫| 亚洲欧美成人网| 国产精品久久久久久久9999| 一区二区欧美国产| 亚洲人在线视频| 欧美国产国产综合| 日韩午夜激情电影| 最新中文字幕一区二区三区| 久久婷婷久久| 亚洲福利视频专区| 久久午夜电影| 麻豆成人在线播放| 亚洲欧洲视频| 亚洲伦理一区| 欧美视频1区| 欧美一级片久久久久久久| 亚洲免费一在线| 国产亚洲欧美另类一区二区三区| 久久国产精品一区二区三区| 亚欧成人在线| 在线观看欧美黄色| 亚洲国产精品激情在线观看| 欧美美女福利视频| 欧美日韩大片一区二区三区| 亚洲福利专区| 亚洲另类春色国产| 欧美日韩在线亚洲一区蜜芽| 亚洲校园激情| 亚洲欧美日本伦理| 国产综合婷婷| 91久久精品国产91久久| 欧美午夜剧场| 久久精品国产999大香线蕉| 久久精品国产77777蜜臀| 亚洲国产老妈| 亚洲午夜精品国产| 在线观看成人网| 日韩亚洲欧美在线观看| 国产亚洲va综合人人澡精品| 欧美华人在线视频| 国产精品女主播| 欧美激情一区| 国产精品久线观看视频| 暖暖成人免费视频| 国产精品嫩草久久久久| 欧美电影在线观看完整版| 国产精品久久久一区麻豆最新章节| 久久中文字幕一区| 国产精品乱码久久久久久| 欧美国产视频在线| 国产亚洲欧美日韩精品| 99亚洲一区二区| 亚洲国产经典视频| 欧美一级片一区| 亚洲一区黄色| 欧美日韩国产小视频在线观看| 久久久国产成人精品| 欧美国产高清| 久久9热精品视频| 欧美大片91| 亚洲精品韩国| 久久蜜桃精品| 亚洲夜间福利| 久久久久久久999| 中文国产成人精品| 亚洲国内自拍| 欧美母乳在线| 两个人的视频www国产精品| 久久久久久久久一区二区| 一区二区视频免费在线观看| 91久久久久久久久| 国模一区二区三区| 91久久精品一区二区三区| 久久久久久亚洲精品中文字幕| 亚洲一区二区在线免费观看视频| 久久精品国产亚洲高清剧情介绍| 亚洲日本成人网| 欧美一级播放| 亚洲在线免费观看| 男人的天堂亚洲| 久久久久91| 欧美国产第二页| 亚洲精品网址在线观看| 在线看片日韩| 香蕉免费一区二区三区在线观看| 日韩一级二级三级| 久久久久久久国产| 欧美在线国产| 在线一区二区三区做爰视频网站| 国产一区二区三区四区老人| 亚洲美女黄色| 亚洲狼人精品一区二区三区| 欧美中文字幕在线观看| 亚洲综合导航| 免费日韩一区二区| 亚洲国内自拍| 亚洲三级免费| 欧美3dxxxxhd| 牛牛影视久久网| 在线免费观看视频一区| 欧美一区二区三区在线免费观看| 亚洲欧美国产毛片在线| 美女精品网站| 亚洲第一精品夜夜躁人人躁| 在线观看欧美亚洲| 久久久精品动漫| 欧美成人免费全部| 亚洲国产精品999| 久久久久一区| 欧美电影在线免费观看网站| 9久re热视频在线精品| 欧美sm视频| 亚洲国产一区二区三区a毛片 | 久久久久久久久久久久久久一区| 香蕉成人伊视频在线观看| 国产欧美欧美| 久久久久久久一区| 欧美国产精品人人做人人爱| 亚洲国产精品日韩| 国产精品毛片a∨一区二区三区|国 | 欧美一区二区在线观看| 久久亚洲图片| 国产视频一区在线| 欧美成人影音| 99在线|亚洲一区二区| 亚洲欧美在线一区二区| 国产女主播一区二区三区| 欧美在线免费看| 欧美aⅴ99久久黑人专区| 亚洲日韩欧美视频一区| 免费在线亚洲| 久久精品国产一区二区三区| 免费不卡欧美自拍视频| 亚洲三级色网| 国产午夜精品全部视频在线播放| 久久久青草青青国产亚洲免观| 欧美成人中文| 亚洲自拍都市欧美小说| 国内精品久久久久伊人av| 欧美黑人国产人伦爽爽爽| 中文国产成人精品| 一本色道久久综合亚洲二区三区 | 亚洲国产99| 亚洲一级一区| 国产精品每日更新| 久久久噜噜噜| 久久久久久久精| 亚洲男人的天堂在线| 亚洲国产精品第一区二区三区| 亚洲一区国产精品| 亚洲国产天堂久久综合网| 国产精品视频专区| 亚洲综合日韩中文字幕v在线| 影音先锋久久资源网| 欧美日韩国产专区| 久久九九热re6这里有精品| 日韩网站免费观看| 蜜臀av性久久久久蜜臀aⅴ| 亚洲欧美一区二区三区极速播放| 国产伦一区二区三区色一情| 欧美成人午夜视频| 久久精品首页| 香蕉免费一区二区三区在线观看 | 久久久久国产精品一区| 夜夜嗨av一区二区三区网站四季av| 欧美福利精品| 亚洲最快最全在线视频| 一区二区三区 在线观看视| 亚洲国产精品第一区二区| 国产在线拍偷自揄拍精品| 国产精品毛片一区二区三区| 欧美日韩国产在线| 欧美成人首页| 香蕉久久久久久久av网站| 欧美在线日韩| 亚洲欧洲99久久| 亚洲一区二区精品在线观看| 日韩视频精品在线| 亚洲激情婷婷| 亚洲黄色精品| 亚洲精品一区二区网址 | 久久综合伊人77777| 欧美怡红院视频一区二区三区| 中日韩午夜理伦电影免费| 亚洲视频高清| 亚洲私人影吧| 亚洲永久在线| 国产精品99久久不卡二区| 一区二区日韩免费看| 久久久久国产精品www|