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

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>
            国内外成人免费激情在线视频 | 暖暖成人免费视频| 亚洲素人一区二区| 99精品视频网| 中日韩男男gay无套| 日韩视频―中文字幕| 亚洲国产欧美一区| 亚洲国产精品激情在线观看| 亚洲缚视频在线观看| 亚洲电影下载| 亚洲色图综合久久| 久久www成人_看片免费不卡| 久久久视频精品| 欧美日韩国产不卡| 国产精品入口福利| 国模精品一区二区三区色天香| 亚洲电影在线| 亚洲午夜国产成人av电影男同| 午夜亚洲视频| 欧美激情一区| 亚洲综合另类| 欧美激情中文字幕乱码免费| 国产精品一香蕉国产线看观看 | 狠狠色狠狠色综合日日小说| 亚洲欧洲一区二区天堂久久 | 亚洲第一黄色网| 国产精品黄色| 亚洲影院色无极综合| 久久精品夜色噜噜亚洲a∨| 欧美激情2020午夜免费观看| 国产精品美腿一区在线看| 1024国产精品| 亚洲男人av电影| 欧美激情一区在线| 欧美一区1区三区3区公司| 欧美激情欧美激情在线五月| 国产一区二区日韩精品| 一片黄亚洲嫩模| 久久免费国产精品| 一区二区久久久久久| 你懂的一区二区| 国产欧美日韩| 亚洲永久网站| 亚洲美女视频网| 蜜臀av一级做a爰片久久| 国产综合视频| 久久国产一二区| 中文亚洲欧美| 欧美日韩综合一区| 一本色道久久综合亚洲精品不| 另类图片国产| 久久国产综合精品| 国产午夜精品久久久久久久| 亚洲欧美制服另类日韩| 99riav久久精品riav| 欧美激情国产日韩精品一区18| 在线免费观看日本欧美| 美女精品在线观看| 久久久欧美一区二区| 激情综合激情| 久久视频在线视频| 久久精品视频一| 国产一区二区精品久久| 久久久综合网站| 久久精品一区二区三区四区| 黄色日韩在线| 欧美激情精品久久久久久| 久久综合五月天婷婷伊人| 亚洲福利小视频| 亚洲国产一区二区三区高清| 欧美阿v一级看视频| 日韩视频中文| 99精品欧美| 国产精品永久免费在线| 久久精品中文字幕一区| 欧美一区二区大片| 在线播放不卡| 亚洲国产激情| 国产精品www| 欧美在线视频一区二区| 久久精品视频va| 亚洲日本视频| 在线综合亚洲欧美在线视频| 国产精品网曝门| 牛人盗摄一区二区三区视频| 欧美精品在线一区| 亚洲国产成人tv| 欲香欲色天天天综合和网| 欧美成人免费大片| 欧美激情综合色| 午夜在线成人av| 久久久91精品国产| 日韩视频免费观看高清完整版| 99国产精品| 狠狠爱综合网| 亚洲精品久久久久久久久久久久久 | 国产亚洲第一区| 亚洲第一毛片| 国产精品视频区| 欧美.www| 国产伦精品一区二区三区免费迷 | 欧美日韩xxxxx| 久久se精品一区精品二区| 久久一区精品| 午夜视频在线观看一区二区| 卡通动漫国产精品| 欧美亚洲一区二区在线| 你懂的亚洲视频| 欧美在线观看一区二区| 欧美激情 亚洲a∨综合| 久久久久久久久久看片| 欧美连裤袜在线视频| 久久久久免费观看| 欧美日韩综合精品| 欧美成人tv| 国产日韩欧美不卡在线| 亚洲人午夜精品免费| 在线观看成人网| 亚洲影院色无极综合| 正在播放亚洲一区| 欧美成人亚洲成人| 久久久久久久久久久久久女国产乱 | 美日韩在线观看| 国产精品久久久久99| 亚洲国产成人在线视频| 一区二区三区自拍| 性欧美videos另类喷潮| 亚洲男人影院| 欧美日韩视频在线观看一区二区三区| 欧美成年人网站| 狠狠噜噜久久| 久久精品观看| 狼人天天伊人久久| 一区二区三区在线看| 国产精品对白刺激久久久| 亚洲男人的天堂在线观看| 亚洲精品一区在线观看香蕉| 久久国产精品久久w女人spa| 久久精品国产在热久久| 国产日韩一区二区三区在线播放| 久久福利影视| 国产日本欧美一区二区三区在线| 亚洲尤物在线视频观看| 午夜精品免费| 国产欧美在线播放| 午夜在线不卡| 久久久久久久久伊人| 好吊视频一区二区三区四区| 性亚洲最疯狂xxxx高清| 久久精品国产亚洲a| 国产视频欧美| 久久成人国产| 欧美国产日产韩国视频| 亚洲区免费影片| 欧美日韩日本视频| 国产精品99久久久久久久vr| 欧美一区高清| 亚洲国产日韩一区| 欧美黑人在线播放| 一区二区三区四区国产| 欧美一区二区三区免费看| 性欧美大战久久久久久久免费观看 | 国产日韩欧美综合在线| 久久久久久婷| 欧美成人免费在线观看| 日韩一级精品| 国产日本欧洲亚洲| 欧美成年人视频网站| 亚洲视屏一区| 欧美成人精品在线观看| 亚洲最新在线视频| 国产欧美一区二区色老头 | 欧美成ee人免费视频| 一区二区久久| 国产午夜精品一区理论片飘花| 噜噜噜躁狠狠躁狠狠精品视频 | 性欧美18~19sex高清播放| 亚洲成人直播| 亚洲在线观看| 亚洲国产美女| 国产精品一区二区三区久久 | 亚洲女优在线| 欧美搞黄网站| 久久久国产精品亚洲一区| 99精品99| 一区在线播放| 国产精品福利av| 欧美a级片网站| 欧美一区1区三区3区公司| 亚洲三级影院| 欧美成人按摩| 久久三级福利| 小处雏高清一区二区三区| 在线综合亚洲| 亚洲永久网站| 亚洲欧洲另类| 极品少妇一区二区| 国产精品毛片一区二区三区| 欧美另类99xxxxx| 免费高清在线一区|