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

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 閱讀(10030) 評論(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>
            一区二区高清视频| 最新中文字幕亚洲| 午夜精品一区二区三区在线视| 欧美黄色小视频| 欧美高清一区二区| 亚洲国产午夜| 亚洲国产欧美一区二区三区同亚洲| 欧美激情中文字幕一区二区| 欧美成人综合在线| 亚洲国产日韩欧美| 日韩网站在线观看| 亚洲一区国产视频| 欧美在线二区| 麻豆精品在线视频| 欧美激情亚洲国产| 欧美午夜精品理论片a级按摩| 国产精品视频男人的天堂| 国产欧美日韩在线视频| 亚洲电影有码| 亚洲欧美色婷婷| 久久久久一本一区二区青青蜜月| 欧美bbbxxxxx| 亚洲视频大全| 美日韩精品视频| 国产精品视频不卡| 亚洲日本电影| 欧美影院成人| 91久久精品久久国产性色也91 | 国产精品99久久久久久白浆小说| 亚洲尤物在线| 欧美专区在线| 亚洲国产高清aⅴ视频| 亚洲精品美女| 久久aⅴ国产紧身牛仔裤| 欧美激情影院| 国内精品久久久久影院色| 最新日韩欧美| 可以免费看不卡的av网站| 亚洲精品少妇网址| 久久精品亚洲一区二区三区浴池| 欧美日本韩国一区| 在线看一区二区| 亚洲少妇在线| 欧美激情一区二区三级高清视频| 亚洲欧美文学| 欧美天堂亚洲电影院在线观看| 亚洲成色最大综合在线| 久久精品论坛| 西西人体一区二区| 国产精品伦子伦免费视频| 日韩视频免费在线| 欧美国产精品日韩| 久久蜜臀精品av| 一区二区在线不卡| 久久香蕉国产线看观看av| 亚洲欧美日韩直播| 国产精品久久久久久久久久久久| 亚洲欧洲美洲综合色网| 蜜乳av另类精品一区二区| 欧美中文日韩| 亚洲国产精品va在线看黑人动漫| 久久久久一本一区二区青青蜜月| 亚洲私人影院在线观看| 欧美四级在线观看| 亚洲一区二区三区777| 99精品热视频| 欧美日韩国产bt| 日韩一区二区精品葵司在线| 亚洲黄色小视频| 欧美日韩成人一区二区| 亚洲小少妇裸体bbw| 亚洲一区3d动漫同人无遮挡| 国产精品亚洲美女av网站| 欧美一区二区三区视频免费播放| 亚洲女性裸体视频| 一区二区视频免费在线观看 | 免费在线成人av| 亚洲国产精品黑人久久久| 欧美好吊妞视频| 欧美日本一道本| 香蕉av福利精品导航| 欧美一区激情| 亚洲国产岛国毛片在线| 亚洲韩国精品一区| 欧美视频在线观看| 久久精品日韩| 免费在线观看日韩欧美| 亚洲日产国产精品| 久久综合九色| 亚洲国产婷婷综合在线精品| 欧美激情a∨在线视频播放| 欧美激情中文不卡| 亚洲欧美成aⅴ人在线观看| 欧美一区三区二区在线观看| 亚洲激情亚洲| 亚洲最新合集| 国产一区二区久久精品| 欧美激情二区三区| 国产精品综合不卡av| 欧美激情日韩| 国产精品在线看| 亚洲电影免费在线| 国产精品视频在线观看| 欧美激情亚洲综合一区| 国产午夜精品久久久久久久| 久久综合中文色婷婷| 欧美久久久久| 久久午夜羞羞影院免费观看| 欧美美女操人视频| 老**午夜毛片一区二区三区| 国产精品分类| 亚洲激情在线播放| 精品福利av| 亚洲欧美一区二区原创| 亚洲精品视频在线播放| 亚洲视频图片小说| 亚洲国产成人av| 亚洲专区一区二区三区| 亚洲精品一二三区| 久久成人国产精品| 洋洋av久久久久久久一区| 久久精品亚洲乱码伦伦中文| 午夜精品久久一牛影视| 午夜精品99久久免费| 亚洲黑丝一区二区| 亚洲免费视频网站| 亚洲影院污污.| 欧美精品尤物在线| 久久国产精品72免费观看| 久久久久久日产精品| 欧美在线中文字幕| 欧美国产三区| 欧美激情第五页| 亚洲国产欧洲综合997久久| 久久国产一区二区| 午夜在线成人av| 国产精品乱码久久久久久| 欧美大片免费久久精品三p| 国产麻豆视频精品| 亚洲欧美一区二区精品久久久| 亚洲五月婷婷| 国产精品久久久久99| 亚洲精品国产品国语在线app| 亚洲第一精品夜夜躁人人爽| 午夜一区二区三区在线观看| 日韩午夜av在线| 欧美成人国产一区二区| 免费亚洲视频| 国产亚洲一级| 久久久久久999| 欧美在线视频观看免费网站| 国产精品视频免费观看| 99成人在线| 久久大逼视频| 一本色道久久综合亚洲91| 久久久国产精品一区| 久久婷婷一区| 玉米视频成人免费看| 久久综合伊人77777尤物| 亚洲成色精品| 亚洲卡通欧美制服中文| 欧美日韩亚洲一区二区| 亚洲一区二区在线视频| 欧美一级欧美一级在线播放| 国产精品毛片a∨一区二区三区| 亚洲美女色禁图| 久久久91精品| 亚洲国产高清在线观看视频| 欧美黄色aaaa| 亚洲女女女同性video| 欧美国产日产韩国视频| 中文国产成人精品久久一| 国产欧美激情| 欧美一级欧美一级在线播放| 久久国产精品久久久久久久久久 | 国产午夜一区二区三区| 欧美在线免费播放| 亚洲国产专区校园欧美| 香蕉成人久久| 亚洲精品美女在线观看| 国产精品稀缺呦系列在线| 蜜桃伊人久久| 亚洲直播在线一区| 欧美福利电影网| 亚洲综合成人婷婷小说| 国产揄拍国内精品对白| 玖玖玖免费嫩草在线影院一区| 亚洲视频在线观看免费| 免费亚洲电影在线| 亚洲无限乱码一二三四麻| 亚洲激情在线激情| 国产一区999| 国产精品大全| 欧美国产精品久久| 久久久久国色av免费观看性色| 亚洲乱码国产乱码精品精天堂| 久久精选视频| 欧美一级视频一区二区| 国产精品99久久不卡二区| 亚洲片区在线|