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

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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

關于C++的 explicit關鍵字

Posted on 2006-02-05 05:16 Tommy Liang 閱讀(10036) 評論(6)  編輯 收藏 引用 所屬分類: C++語言與規范

簡而言之:explicit修飾的構造函數不能擔任轉換函數

這個 《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: 關于C++的 explicit關鍵字  回復  更多評論   

2006-04-18 10:45 by youtr
呵呵,寫的不錯

# re: 關于C++的 explicit關鍵字  回復  更多評論   

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: 關于C++的 explicit關鍵字  回復  更多評論   

2008-06-20 11:13 by Anecdote
學習了。謝謝

# re: 關于C++的 explicit關鍵字[未登錄]  回復  更多評論   

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

# re: 關于C++的 explicit關鍵字  回復  更多評論   

2009-03-16 10:35 by inzer
不錯

# re: 關于C++的 explicit關鍵字  回復  更多評論   

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>
            久久人人爽人人爽| 黄色在线一区| 欧美专区日韩视频| 久久成人综合视频| 久久久精品一区| 欧美aaaaaaaa牛牛影院| 欧美激情1区2区| 欧美日韩午夜剧场| 欧美亚洲成人精品| 国产日韩欧美在线一区| 韩日精品视频| 亚洲激情视频在线播放| 一区二区三区国产| 欧美中文日韩| 亚洲国产成人在线播放| 欧美成人一区在线| 亚洲最快最全在线视频| 欧美一区二区成人| 欧美不卡在线| 国产欧美日本在线| 亚洲激情在线观看视频免费| 亚洲女性裸体视频| 欧美大片在线看免费观看| 亚洲免费av片| 久久综合九色综合欧美就去吻 | 亚洲欧美在线网| 美女露胸一区二区三区| 亚洲乱码国产乱码精品精天堂 | 久久久999精品| 欧美精品激情| 国精产品99永久一区一区| 亚洲精品国产拍免费91在线| 欧美主播一区二区三区| 亚洲日本欧美日韩高观看| 午夜久久久久久| 欧美激情一区二区三区蜜桃视频| 国产美女精品一区二区三区| 日韩视频在线观看免费| 久久久噜噜噜久久中文字幕色伊伊| 亚洲老板91色精品久久| 久久青草久久| 国模套图日韩精品一区二区| 亚洲欧美电影院| 99亚洲伊人久久精品影院红桃| 久久亚洲不卡| 一区二区三区在线视频观看| 性欧美大战久久久久久久久| 亚洲人成在线观看网站高清| 99re热这里只有精品免费视频| 国语自产偷拍精品视频偷 | 免费成人高清视频| 亚洲午夜电影网| 欧美日韩不卡一区| 亚洲三级色网| 美女日韩在线中文字幕| 久久国产精品久久久久久久久久| 国产精品扒开腿做爽爽爽软件 | 久久艳片www.17c.com| 亚洲伊人伊色伊影伊综合网| 欧美性猛交xxxx乱大交退制版| 99精品欧美一区二区三区| 亚洲国产99| 欧美激情日韩| 亚洲免费电影在线| 亚洲精品日韩久久| 欧美日韩视频在线第一区| 亚洲美女电影在线| 亚洲激情视频在线观看| 欧美精品久久久久久久| 99视频在线精品国自产拍免费观看 | 国产日韩av高清| 久久成人免费日本黄色| 欧美在线亚洲一区| 国内伊人久久久久久网站视频| 久久亚洲欧美| 欧美 亚欧 日韩视频在线| 亚洲狼人综合| 艳妇臀荡乳欲伦亚洲一区| 国产精品理论片在线观看| 久久久国产精品亚洲一区| 久久高清福利视频| 亚洲国产高清高潮精品美女| 亚洲经典视频在线观看| 欧美日韩在线免费观看| 西西裸体人体做爰大胆久久久| 亚洲伊人网站| 最近中文字幕mv在线一区二区三区四区 | 国产精品一级| 久久国产精品第一页| 久久久精品国产一区二区三区 | 亚洲一区成人| 欧美一级播放| 伊人狠狠色丁香综合尤物| 欧美国产丝袜视频| 欧美无乱码久久久免费午夜一区| 欧美一区二区三区免费大片| 国内揄拍国内精品久久| 亚洲精品欧美在线| 亚洲最新视频在线| 国产一区二区三区久久久| 欧美第一黄网免费网站| 欧美日韩系列| 牛人盗摄一区二区三区视频| 欧美视频导航| 欧美成人综合| 国产日韩欧美三级| 亚洲精品在线一区二区| 经典三级久久| 亚洲视频1区| 亚洲精品欧洲精品| 久久精品国产在热久久| 99视频精品免费观看| 久久精品夜色噜噜亚洲a∨| 亚洲午夜精品一区二区| 老司机午夜精品视频在线观看| 欧美亚洲免费高清在线观看| 欧美黄色一级视频| 久久人体大胆视频| 国产精品一区二区在线观看不卡| 亚洲电影在线播放| 伊人成人开心激情综合网| 在线视频日韩精品| 日韩视频在线观看免费| 另类av一区二区| 亚洲欧美在线播放| 欧美日韩在线第一页| 亚洲国产精品免费| 在线日韩欧美视频| 久久国产高清| 久久成人精品电影| 国产欧美日韩伦理| 亚洲一区二区三区四区五区午夜 | 久久精品在线观看| 国产视频自拍一区| 亚洲专区一二三| 亚洲欧美日韩一区二区三区在线| 欧美精品一区二区精品网| 亚洲国产裸拍裸体视频在线观看乱了中文 | 亚洲一区二区网站| 亚洲一区二区高清| 欧美三级免费| 这里只有精品丝袜| 亚洲一区在线播放| 欧美日韩在线精品| 一区二区三区国产| 亚洲男女毛片无遮挡| 欧美网站在线| 亚洲欧美日韩国产| 久久久久国产精品人| 国语自产精品视频在线看抢先版结局 | 亚洲国产视频一区| 国产精品夜夜夜| 亚洲综合视频在线| 欧美中文字幕在线视频| 韩日欧美一区二区| 免费在线日韩av| 99精品国产在热久久下载| 亚洲女爱视频在线| 韩国成人理伦片免费播放| 毛片基地黄久久久久久天堂| 91久久精品一区二区别| 亚洲一区二区三区精品动漫| 国产欧美日韩高清| 美脚丝袜一区二区三区在线观看| 最新成人在线| 亚洲欧美日韩精品久久久久| 国产亚洲欧美一区二区| 久久婷婷人人澡人人喊人人爽| 亚洲电影在线看| 亚洲一二三级电影| 国产亚洲第一区| 免费欧美视频| 午夜老司机精品| 亚洲欧洲中文日韩久久av乱码| 亚洲欧美日本国产有色| 一区在线免费| 欧美色图天堂网| 久久久一区二区三区| 亚洲精品一区二区三区樱花| 欧美在线视频播放| 亚洲精品国产拍免费91在线| 国产精品美女久久久久久2018| 久久精品免费电影| 日韩一级在线| 蜜桃久久精品乱码一区二区| 亚洲午夜av| 亚洲国产专区校园欧美| 国产精品一区二区a| 欧美v日韩v国产v| 羞羞色国产精品| 亚洲伦理在线免费看| 奶水喷射视频一区| 欧美一区二区三区在线观看| 亚洲全黄一级网站| 国内精品一区二区三区| 国产精品av一区二区| 欧美激情第10页| 久久视频在线看| 久久久久99| 欧美一区二区视频免费观看|