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

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 閱讀(10035) 評論(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>
            欧美在线播放一区| 亚洲欧美制服另类日韩| 西西人体一区二区| 国产一区二区三区高清在线观看| 欧美一区二区在线观看| 久久精品女人天堂| 夜久久久久久| 久久精品日韩| 亚洲校园激情| 久久精品盗摄| 在线天堂一区av电影| 久久高清福利视频| 一区二区三欧美| 久久精品视频99| 一区二区三区日韩在线观看 | 在线观看欧美视频| 快射av在线播放一区| 久久深夜福利免费观看| 亚洲香蕉伊综合在人在线视看| 欧美一区二区三区视频在线| 亚洲精品字幕| 午夜在线电影亚洲一区| 中文亚洲欧美| 欧美国产视频在线| 久久躁日日躁aaaaxxxx| 欧美日韩在线亚洲一区蜜芽| 欧美激情第3页| 精品av久久久久电影| 亚洲夜间福利| 亚洲一区二区三区三| 欧美久久电影| 亚洲电影免费观看高清| 国产精品久久久久国产a级| 亚洲国产老妈| 伊人久久亚洲美女图片| 亚洲男人第一av网站| 中文日韩在线| 欧美日韩八区| 亚洲全部视频| 99成人在线| 欧美精品日本| 亚洲黄色成人网| 亚洲另类视频| 欧美区高清在线| 亚洲精品乱码久久久久| 亚洲国产清纯| 久久综合久久久久88| 欧美96在线丨欧| 极品尤物av久久免费看| 久久riav二区三区| 免费亚洲电影在线| 亚洲人成高清| 欧美日韩成人在线观看| 亚洲精品中文字| 亚洲性感激情| 国产精品va在线播放我和闺蜜| 亚洲精选91| 亚洲视频一区在线观看| 欧美日韩在线视频一区二区| 亚洲最黄网站| 性欧美超级视频| 国产欧美日本一区二区三区| 欧美一区二区三区四区在线观看地址 | 日韩亚洲在线| 欧美三级网址| 亚洲午夜精品| 久久久蜜臀国产一区二区| 一区二区在线视频| 乱码第一页成人| 亚洲精品在线一区二区| 午夜日韩视频| 精品69视频一区二区三区| 嫩草伊人久久精品少妇av杨幂| 亚洲精品视频在线播放| 亚洲欧美久久久| 伊人激情综合| 欧美日韩国产不卡| 午夜精品偷拍| 亚洲大片在线| 午夜欧美不卡精品aaaaa| 精品99一区二区三区| 欧美日本高清视频| 亚洲欧美另类中文字幕| 欧美a级片网站| 亚洲一区二区三区777| 狠狠久久综合婷婷不卡| 欧美国产欧美综合| 羞羞答答国产精品www一本| 欧美二区在线| 亚洲一区二区三区久久| 在线观看三级视频欧美| 欧美三级午夜理伦三级中视频| 欧美一区二区三区喷汁尤物| 亚洲国产欧美日韩另类综合| 欧美一区2区视频在线观看| 在线看片成人| 国产欧美精品一区二区三区介绍 | 国产中文一区二区| 免费在线看成人av| 亚洲欧美成人精品| 亚洲电影免费观看高清完整版在线观看| 亚洲香蕉网站| 亚洲国产精品成人精品| 国产精品久久夜| 欧美国产高潮xxxx1819| 午夜精品久久久久久久男人的天堂 | 亚洲国产成人高清精品| 欧美日韩在线免费| 免费成人av| 午夜精品999| 日韩一本二本av| 欧美激情久久久久久| 久久久国产一区二区三区| 亚洲视频免费| 亚洲精品久久久久中文字幕欢迎你 | 亚洲视频欧洲视频| 亚洲乱码国产乱码精品精可以看 | 性欧美xxxx视频在线观看| 一级成人国产| 亚洲免费观看视频| 91久久中文| 亚洲国产成人tv| 欧美3dxxxxhd| 每日更新成人在线视频| 性娇小13――14欧美| 国产伦精品一区二区三区免费| 欧美日韩国产高清视频| 欧美v亚洲v综合ⅴ国产v| 久久黄色小说| 欧美一区二区三区在| 亚洲欧美日韩天堂一区二区| 一本大道久久a久久精品综合 | 在线播放国产一区中文字幕剧情欧美| 国产欧美大片| 国产香蕉97碰碰久久人人| 国产精品美女999| 国产精品久久久999| 欧美日一区二区三区在线观看国产免| 欧美精品123区| 欧美日韩系列| 国产精品sm| 国产日韩欧美综合精品| 国产欧美视频一区二区三区| 国产美女高潮久久白浆| 国产午夜精品全部视频在线播放| 国产精品高潮呻吟久久av无限 | 久久久另类综合| 久久中文欧美| 久久久久国产精品一区三寸| 久久久久久久91| 免费视频亚洲| 亚洲精品一区二区在线| 一区二区精品在线| 香蕉久久夜色精品国产使用方法| 午夜精品久久久久久久久| 久久精品亚洲精品| 欧美成人精品三级在线观看| 欧美国产日韩一二三区| 欧美日韩国产成人在线| 国产精品视频久久| 狠狠干综合网| 日韩网站免费观看| 亚洲尤物视频网| 久久久久国内| 亚洲高清av| 亚洲欧美成人精品| 裸体一区二区三区| 欧美日韩在线视频观看| 国产婷婷97碰碰久久人人蜜臀| 在线不卡亚洲| 亚洲天堂av图片| 久久人人爽人人爽爽久久| 亚洲大胆av| 亚洲欧美中日韩| 女女同性精品视频| 欧美日韩一区二区三区在线看| 国产精品青草综合久久久久99| 狠狠久久五月精品中文字幕| 国产精品99久久久久久久久| 久久黄色级2电影| 欧美激情中文字幕一区二区| 亚洲校园激情| 欧美国产视频日韩| 国语精品一区| 亚洲在线观看免费视频| 免费在线观看一区二区| 99在线精品视频在线观看| 欧美主播一区二区三区| 欧美色区777第一页| 黄色一区二区在线观看| 亚洲图片欧洲图片日韩av| 久久亚洲精品伦理| 亚洲免费播放| 美女视频黄 久久| 国产真实乱子伦精品视频| 亚洲在线免费观看| 亚洲国产经典视频| 久久国内精品自在自线400部| 欧美视频在线视频| 亚洲激情电影在线|