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

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>
            亚久久调教视频| 国产小视频国产精品| 欧美va亚洲va国产综合| 欧美午夜美女看片| 亚洲欧洲免费视频| 久久天堂国产精品| 久久精品国产亚洲5555| 国产亚洲福利| 久久国产视频网| 亚洲午夜精品一区二区三区他趣| 免费在线欧美黄色| 伊人久久亚洲影院| 久久久久久久久综合| 亚洲欧美一区二区三区在线| 国产精品久久7| 亚洲一区二区三区在线播放| 亚洲免费电影在线| 欧美连裤袜在线视频| 亚洲精品视频一区| 亚洲国产一区二区在线| 免费视频最近日韩| 亚洲激情另类| 亚洲欧洲三级电影| 欧美日韩免费观看一区二区三区| 99一区二区| 亚洲一级黄色片| 亚洲美女毛片| 国产精品久久激情| 欧美在线视频一区| 久久久久久一区| 亚洲毛片在线观看| 亚洲香蕉网站| 激情一区二区| 亚洲三级性片| 国产欧美日韩激情| 免费不卡在线视频| 欧美成人午夜免费视在线看片| 中日韩男男gay无套| 欧美一区国产在线| 91久久午夜| av成人免费| 亚洲欧洲精品一区二区三区不卡 | 欧美性一区二区| 一区二区国产在线观看| 久久精品一区二区三区中文字幕| 一区二区三区高清| 欧美激情第六页| 夜夜嗨av色综合久久久综合网| 夜夜爽av福利精品导航| 欧美日韩免费视频| 亚洲欧美日韩精品久久亚洲区| 欧美一二三区精品| 永久久久久久| 欧美日本在线视频| 99视频一区二区三区| 久久精彩视频| 9i看片成人免费高清| 国产小视频国产精品| 欧美成人精品高清在线播放| 亚洲精品久久| 欧美在线www| 亚洲欧洲一区二区在线播放 | 欧美一二区视频| 免费在线视频一区| 亚欧成人精品| 亚洲一二三四久久| 亚洲高清电影| 国产主播一区二区| 国产精品人人做人人爽| 欧美伦理视频网站| 免费在线观看一区二区| 久久久久久综合网天天| 亚洲影视综合| 亚洲在线观看视频网站| 最新亚洲电影| 国产精品国色综合久久| 欧美一区二区三区免费观看视频 | 一区二区三区国产精华| 亚洲国产精品第一区二区| 国内精品视频久久| 1024日韩| 亚洲精品孕妇| 99热免费精品| 午夜久久资源| 欧美在线视频免费播放| 美女网站在线免费欧美精品| 欧美亚洲一区二区三区| 久久色在线播放| 欧美成人精品激情在线观看| 嫩草伊人久久精品少妇av杨幂| 麻豆国产精品一区二区三区 | 欧美一区二区三区免费看| 亚洲欧美激情视频在线观看一区二区三区| 亚洲视频综合在线| 久久激情视频免费观看| 欧美有码视频| 亚洲九九九在线观看| 亚洲无线观看| 久久艳片www.17c.com| 欧美日韩精品是欧美日韩精品| 国产精品女人网站| 亚洲第一精品夜夜躁人人爽| 午夜亚洲精品| 中文日韩欧美| 亚洲欧美日韩在线不卡| 久久久久久一区二区| 亚洲国产精品久久久久婷婷884| 久久久国产一区二区三区| 欧美一区二区精品在线| 牛牛国产精品| 亚洲国产精品免费| 欧美jizz19hd性欧美| 午夜在线成人av| 欧美日韩在线直播| 亚洲性视频网站| 中文欧美在线视频| 国产精品美女久久久免费| 香蕉成人啪国产精品视频综合网| 欧美在线免费一级片| 一区二区三区在线免费观看| 久久精品一区四区| 香蕉久久夜色精品国产| 国产精品视频一区二区三区| 亚洲欧美日本视频在线观看| 欧美激情视频网站| 久热精品在线| 亚洲乱码国产乱码精品精98午夜 | 欧美高清在线视频| 欧美一二区视频| 一区免费在线| 日韩午夜激情av| 久久久九九九九| 一本色道久久综合亚洲精品高清| 亚洲神马久久| 99视频国产精品免费观看| 亚洲视频在线观看视频| 国产精品久久久久一区二区三区共| 日韩视频免费在线观看| 亚洲伊人色欲综合网| 亚洲精品国产精品国自产观看| 亚洲欧美三级在线| 亚洲国产精品国自产拍av秋霞| 亚洲激情av| 国产精品麻豆欧美日韩ww | 欧美成人免费网| 欧美风情在线观看| 亚洲视频一区在线| 欧美一区2区三区4区公司二百| 亚洲经典一区| 免费试看一区| 亚洲福利视频三区| 亚洲天堂免费在线观看视频| 国产综合在线看| 欧美一区二区三区啪啪| 亚洲欧洲偷拍精品| 美女久久网站| 美国十次了思思久久精品导航| 欧美激情精品久久久久久黑人 | 亚洲高清免费视频| 欧美福利视频| 午夜精品理论片| 亚洲精品国产视频| 久久一区二区精品| 亚洲一级片在线观看| 亚洲国产精品久久久久秋霞不卡 | 亚洲精选一区| 精品动漫av| 久久精品国产免费观看| 午夜精品久久久| 国产精品久久久久久久久借妻| 欧美激情2020午夜免费观看| 亚洲精品在线一区二区| 美女黄毛**国产精品啪啪| 亚洲国产精彩中文乱码av在线播放| 国产日韩视频| 麻豆国产精品va在线观看不卡| 欧美gay视频| 亚洲国产精品久久久久久女王| 欧美日韩中文字幕在线| 久久久高清一区二区三区| 亚洲国产精品999| 亚洲一级网站| 日韩一本二本av| 国产精品www| 免费成人美女女| 亚洲婷婷在线| 亚洲精品网站在线播放gif| 久久精品视频va| 亚洲午夜av| 国产精品99久久久久久久久| 国产日韩欧美视频| 欧美日韩美女一区二区| 中文亚洲免费| 国产午夜亚洲精品理论片色戒| 久久久欧美精品sm网站| 亚洲手机成人高清视频| 在线一区二区三区做爰视频网站 | 韩国女主播一区| 欧美日韩成人在线播放| 久久久精品2019中文字幕神马|