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

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>
            亚洲激情在线观看视频免费| 精品999久久久| 国产女人aaa级久久久级| 亚洲国产成人一区| 久久久99久久精品女同性| 亚洲欧洲精品一区| 性久久久久久久久久久久| 欧美日韩一区二区视频在线观看| 在线日本欧美| 两个人的视频www国产精品| 午夜精品三级视频福利| 国产精品免费网站在线观看| 亚洲午夜91| 一本大道久久a久久综合婷婷| 美女日韩欧美| 亚洲国产精品福利| 欧美激情亚洲激情| 欧美国产综合视频| 日韩一级视频免费观看在线| 亚洲人成在线播放| 欧美另类在线播放| 在线综合欧美| 亚洲午夜精品视频| 国产欧美丝祙| 久热精品视频在线免费观看| 久久久亚洲欧洲日产国码αv| 尤物yw午夜国产精品视频| 免费视频亚洲| 欧美激情精品久久久| 亚洲视频axxx| 亚洲小视频在线| 国产日韩精品视频一区二区三区| 久久综合久久综合久久| 麻豆亚洲精品| 亚洲美女黄网| 亚洲专区免费| 在线精品国产欧美| 亚洲国产精品小视频| 欧美日韩亚洲网| 欧美中文字幕在线播放| 久久免费一区| 亚洲视频欧美在线| 欧美中在线观看| 久久精品国产v日韩v亚洲 | 久久久久免费视频| 亚洲国产精品一区| 日韩视频永久免费观看| 国产亚洲成av人片在线观看桃 | 在线一区二区三区四区| 国产欧美91| 亚洲国产精品成人综合色在线婷婷 | 亚洲精品乱码久久久久久按摩观| 国产精品久久久久国产a级| 久久久女女女女999久久| 欧美精品一线| 久久尤物视频| 国产精品成人免费精品自在线观看| 久久九九电影| 欧美日韩性生活视频| 久久久噜噜噜久久狠狠50岁| 欧美精品少妇一区二区三区| 久久久精品一品道一区| 欧美另类综合| 美女国产一区| 国产欧美日韩一区| 亚洲乱码精品一二三四区日韩在线| 国产一区二区三区不卡在线观看| 亚洲精一区二区三区| 1000部国产精品成人观看| 亚洲女女女同性video| 一区二区久久| 欧美aa在线视频| 久久久999精品| 国产精品久久久久久福利一牛影视 | 亚洲精品人人| 久久在线免费| 欧美一区二区三区视频在线| 欧美日韩国产成人在线| 免费观看30秒视频久久| 国产亚洲一区二区精品| 亚洲一区自拍| 亚洲欧美日本伦理| 欧美偷拍另类| 亚洲精选在线观看| av成人黄色| 欧美日韩国产在线一区| 亚洲精品国产精品国自产观看浪潮| 亚洲第一网站| 美女国产精品| 亚洲国产经典视频| 日韩午夜电影| 欧美日韩国产影片| 9色国产精品| 亚洲一区二区三区成人在线视频精品 | 亚洲日本一区二区| 亚洲黄网站在线观看| 久久人人爽人人爽| 在线观看国产欧美| 亚洲精品1区2区| 亚洲免费福利视频| 欧美精品18| 亚洲九九九在线观看| 亚洲欧美日本国产专区一区| 国产精品一区二区你懂的| 亚洲欧美日韩国产| 久久久久久久久综合| 伊人久久综合| 欧美福利一区| 一区二区欧美日韩| 性伦欧美刺激片在线观看| 国产麻豆精品theporn| 欧美主播一区二区三区| 欧美成人资源网| 亚洲无限av看| 国产欧美日韩三区| 美女诱惑黄网站一区| 国产精品毛片大码女人| 夜夜嗨av一区二区三区网站四季av | 亚洲国产欧美在线| 欧美日韩国产麻豆| 亚洲特级片在线| 久久久夜夜夜| 亚洲免费av电影| 国产精品中文字幕在线观看| 久久久av水蜜桃| 亚洲激情欧美| 欧美亚洲日本网站| 伊人影院久久| 欧美日韩精品欧美日韩精品| 午夜激情久久久| 亚洲国产精品成人精品| 亚洲欧美综合一区| 亚洲国产精品电影| 国产精品视频yy9299一区| 久久久精品日韩| 99re66热这里只有精品3直播 | 久久亚洲捆绑美女| 一本色道久久加勒比88综合| 久久久亚洲国产美女国产盗摄| 亚洲精品一二三区| 国产三级精品在线不卡| 欧美精品日本| 久久精品国产成人| 亚洲香蕉伊综合在人在线视看| 欧美福利专区| 久久久www成人免费无遮挡大片| 亚洲精品免费观看| 国产一区91| 国产精品劲爆视频| 欧美乱大交xxxxx| 狼人天天伊人久久| 午夜一级久久| 一本色道久久88精品综合| 欧美电影免费观看| 久久aⅴ国产欧美74aaa| 亚洲午夜激情网站| 91久久精品一区| 国产色综合久久| 欧美亚一区二区| 欧美精选午夜久久久乱码6080| 久久精品国产综合| 先锋影音一区二区三区| 亚洲免费观看| 91久久夜色精品国产九色| 国产精品视频一二| 亚洲一区二区三区影院| 亚洲国产精品一区二区三区| 久久精品女人天堂| 欧美一区二区在线观看| 亚洲午夜久久久| 国产精品99久久久久久宅男| 99热免费精品| 99视频一区二区三区| 亚洲精品国产精品久久清纯直播 | 91久久午夜| 亚洲大片av| 亚洲高清在线播放| 永久91嫩草亚洲精品人人| 激情久久久久久久久久久久久久久久 | 一本色道久久88精品综合| 亚洲精品一区二区三区樱花| 亚洲人人精品| 亚洲区国产区| 亚洲精品中文字幕在线| 亚洲精品国产精品乱码不99按摩 | 亚洲高清视频一区二区| 亚洲电影免费| 亚洲国产美女| 亚洲区在线播放| 999亚洲国产精| 99热在这里有精品免费| 一区二区日韩精品| 亚洲综合不卡| 欧美一区二区三区久久精品茉莉花| 午夜精品久久久久久久男人的天堂| 午夜精品久久久久久99热| 久久国产精品免费一区| 欧美在线日韩精品| 久久久久久久精| 欧美激情综合五月色丁香|