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

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)系 :: 聚合  :: 管理

關于C++的 explicit關鍵字

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

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

這個 《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>
            国产精品私拍pans大尺度在线| 亚洲高清在线观看一区| 9l国产精品久久久久麻豆| 午夜亚洲福利| 亚洲一区在线直播| 日韩亚洲欧美精品| 亚洲电影av| 亚洲美女少妇无套啪啪呻吟| 99在线精品视频在线观看| 亚洲麻豆视频| 99国产精品久久久久老师| 亚洲欧美日本在线| 亚洲私人影院| 性色av香蕉一区二区| 午夜亚洲一区| 裸体一区二区| 欧美精品自拍偷拍动漫精品| 国产精品国产三级国产专播精品人| 国产精品亚洲人在线观看| 激情91久久| 一区二区三区高清视频在线观看| 欧美在线免费观看| 亚洲国产精品电影| 日韩亚洲欧美成人一区| 欧美在线观看一区二区三区| 欧美人成网站| 在线播放亚洲一区| 香蕉乱码成人久久天堂爱免费| 久久综合影视| 亚洲作爱视频| 另类激情亚洲| 国产亚洲精品久久久| 日韩午夜av电影| 久久精品国产亚洲aⅴ| 亚洲人妖在线| 午夜宅男久久久| 另类专区欧美制服同性| 亚洲另类自拍| 麻豆成人在线播放| 国产亚洲一区二区在线观看| 正在播放欧美一区| 欧美777四色影视在线| 亚洲午夜精品福利| 欧美伦理在线观看| 在线色欧美三级视频| 欧美一区二区私人影院日本| 亚洲另类自拍| 欧美国产日韩精品免费观看| 精品成人在线观看| 欧美伊人久久| 亚洲一级网站| 国产精品久久久久久久久动漫| 亚洲国内精品| 蜜臀av一级做a爰片久久| 一本色道久久综合精品竹菊| 欧美二区在线播放| 亚洲人成网站999久久久综合| 久久精品91久久久久久再现| 99re视频这里只有精品| 欧美激情精品久久久久| 亚洲大胆美女视频| 欧美综合激情网| 亚洲一区二区视频在线| 欧美日韩小视频| 亚洲另类春色国产| 亚洲人午夜精品| 久热成人在线视频| 精品91久久久久| 噜噜噜91成人网| 先锋资源久久| 国产精品国产三级国产| 午夜视频精品| 亚洲欧美日韩国产| 国产欧美日韩在线| 老司机精品导航| 老色鬼久久亚洲一区二区| 91久久极品少妇xxxxⅹ软件| 亚洲电影免费观看高清完整版在线观看 | 国产亚洲成精品久久| 亚洲精品乱码久久久久久日本蜜臀 | 午夜一级在线看亚洲| 亚洲日韩欧美视频一区| 亚洲精品久久久久久久久久久久| 欧美国产日韩xxxxx| 亚洲国产视频一区二区| 亚洲国产精品高清久久久| 欧美日韩国产大片| 亚洲欧美一区二区三区极速播放| 亚洲小视频在线| 黄色成人免费网站| 亚洲国产日韩欧美在线图片| 欧美午夜精品久久久久久久| 亚洲欧美中文字幕| 欧美在线www| 亚洲第一主播视频| 亚洲卡通欧美制服中文| 国产精品日韩在线| 久久国产视频网站| 另类尿喷潮videofree| 亚洲欧美精品一区| 久久久人成影片一区二区三区| 一本大道久久a久久精品综合| 在线亚洲自拍| 亚洲激情在线视频| 午夜伦理片一区| 最新日韩av| 亚洲欧美不卡| 在线中文字幕不卡| 欧美一级成年大片在线观看| 亚洲精品综合| 久久国产精品一区二区三区四区| 一本大道久久a久久综合婷婷| 亚洲欧美激情一区二区| 亚洲美女一区| 欧美中文字幕在线视频| 一二三四社区欧美黄| 欧美诱惑福利视频| 亚洲综合清纯丝袜自拍| 欧美华人在线视频| 欧美+日本+国产+在线a∨观看| 国产一区二区三区免费在线观看| 在线一区二区三区四区| 一区二区精品国产| 欧美黄色视屏| 亚洲国产精品久久久| 国精产品99永久一区一区| 亚洲午夜高清视频| 亚洲免费在线观看| 欧美日韩一区二区三| 亚洲高清在线精品| 亚洲激情婷婷| 欧美成人精品激情在线观看| 免费亚洲电影在线| 国产在线精品一区二区夜色| 亚洲综合精品自拍| 亚洲欧美日韩视频一区| 欧美系列电影免费观看| 一区二区av| 在线视频精品一区| 欧美日韩国产一区二区三区地区| 免费日韩成人| 亚洲人体1000| 欧美高清在线视频观看不卡| 91久久精品日日躁夜夜躁国产| 亚洲国产欧美日韩精品| 欧美高清视频| 99国产精品国产精品毛片| 在线一区亚洲| 欧美揉bbbbb揉bbbbb| 欧美大片一区二区| 麻豆精品91| 国模私拍视频一区| 久久国产手机看片| 乱中年女人伦av一区二区| 136国产福利精品导航网址| 免费不卡视频| 99精品国产热久久91蜜凸| 亚洲综合999| 国产亚洲欧美一区二区| 久久三级福利| 一本久道久久综合狠狠爱| 欧美诱惑福利视频| 亚洲高清一区二区三区| 欧美日韩免费一区二区三区| 亚洲午夜电影网| 免费不卡中文字幕视频| 一本色道久久综合亚洲精品婷婷| 国产精品乱码一区二区三区| 欧美一二三视频| 亚洲欧洲精品天堂一级| 亚洲综合精品四区| 亚洲大胆人体在线| 国产精品免费看久久久香蕉| 久久久久国色av免费观看性色| 亚洲精品欧美日韩| 久久精品在线观看| 99日韩精品| 激情综合电影网| 欧美涩涩视频| 另类专区欧美制服同性| 亚洲视频在线一区观看| 女人色偷偷aa久久天堂| 亚洲视频一区| 好男人免费精品视频| 欧美日韩精品免费观看视频| 欧美在线一区二区三区| 99伊人成综合| 亚洲大胆女人| 久久久久久久综合色一本| 亚洲图片激情小说| 亚洲国产日韩美| 国产欧美婷婷中文| 欧美日韩一区在线| 久久午夜电影网| 午夜久久久久久| 在线亚洲成人| 一区二区三区四区五区视频 | 亚洲综合日韩| 最新高清无码专区| 黄色免费成人|