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

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

簡而言之:explicit修飾的構(gòu)造函數(shù)不能擔(dān)任轉(zhuǎn)換函數(shù)

這個(gè) 《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: 關(guān)于C++的 explicit關(guān)鍵字  回復(fù)  更多評論   

2006-04-18 10:45 by youtr
呵呵,寫的不錯(cuò)

# re: 關(guān)于C++的 explicit關(guān)鍵字  回復(fù)  更多評論   

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: 關(guān)于C++的 explicit關(guān)鍵字  回復(fù)  更多評論   

2008-06-20 11:13 by Anecdote
學(xué)習(xí)了。謝謝

# re: 關(guān)于C++的 explicit關(guān)鍵字[未登錄]  回復(fù)  更多評論   

2009-02-18 15:23 by Lisa
講的很透徹!

# re: 關(guān)于C++的 explicit關(guān)鍵字  回復(fù)  更多評論   

2009-03-16 10:35 by inzer
不錯(cuò)

# re: 關(guān)于C++的 explicit關(guān)鍵字  回復(fù)  更多評論   

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>
            亚洲风情在线资源站| 亚洲自拍偷拍网址| 亚洲一二三区在线| 一区二区日韩伦理片| 亚洲一区二区少妇| 欧美怡红院视频| 久久久久久夜精品精品免费| 久久久美女艺术照精彩视频福利播放| 久久裸体艺术| 亚洲人成欧美中文字幕| 一本一本久久| 久久精品国产清自在天天线 | 欧美一区二区视频在线观看2020| 亚洲在线视频网站| 久久国产色av| 欧美区国产区| 国产精品久久久久久超碰| 国产一区二区精品久久99| 亚洲国产综合在线看不卡| 中日韩午夜理伦电影免费| 久久视频一区二区| 一二美女精品欧洲| 久久综合久久综合久久| 国产精品视频精品视频| 亚洲日本中文字幕区| 午夜国产不卡在线观看视频| 牛牛国产精品| 亚洲女人天堂av| 欧美精品日韩一区| 狠狠色狠狠色综合日日小说| 亚洲午夜精品网| 毛片基地黄久久久久久天堂| 宅男噜噜噜66一区二区66| 麻豆精品国产91久久久久久| 国产精品天美传媒入口| 99精品国产高清一区二区| 久久综合色88| 亚洲欧美日韩视频二区| 欧美日韩精品一区二区在线播放 | 一区二区视频免费完整版观看| 国产精品美女久久久久久免费| 欧美激情乱人伦| 午夜精品一区二区三区在线播放| 欧美精品成人91久久久久久久| 狠狠入ady亚洲精品经典电影| 亚洲欧美国产三级| 日韩视频免费观看高清在线视频| 欧美91大片| 亚洲私人影院| 欧美日韩国产a| 亚洲日本国产| 欧美a级一区| 玖玖视频精品| 亚洲日本一区二区| 91久久精品国产91久久性色tv | 欧美激情一区在线| 亚洲激情在线激情| 欧美激情va永久在线播放| 久久夜色精品亚洲噜噜国产mv | 性一交一乱一区二区洋洋av| 最新精品在线| 欧美人与性动交α欧美精品济南到 | 久久久噜噜噜久久中文字免| 国产一区二区成人| 久久深夜福利| 久热精品在线| 一本久久综合亚洲鲁鲁五月天| 亚洲国产欧美一区二区三区久久| 久久蜜桃精品| 日韩午夜电影av| 99视频在线精品国自产拍免费观看 | 久久久久久69| 亚洲国产精品毛片| 亚洲人成网在线播放| 亚洲激情自拍| 欧美日韩福利在线观看| 亚洲欧美成人一区二区三区| 亚洲欧美另类中文字幕| 国产一区二区三区黄视频| 久久伊人免费视频| 欧美大胆成人| 亚洲女ⅴideoshd黑人| 欧美一区二区三区男人的天堂| 国产一区二区三区在线观看视频| 美女啪啪无遮挡免费久久网站| 欧美成人亚洲成人日韩成人| 亚洲午夜高清视频| 欧美一区二区三区四区在线| 亚洲电影免费在线| 夜夜爽夜夜爽精品视频| 国产亚洲欧美激情| 91久久一区二区| 国产专区综合网| 亚洲免费成人| 9国产精品视频| 久久一区国产| 亚洲私人影吧| 久久人人九九| 亚洲自拍偷拍网址| 久久久中精品2020中文| 亚洲免费一区二区| 美女尤物久久精品| 欧美资源在线| 欧美图区在线视频| 欧美国产精品v| 国产日产欧美a一级在线| 亚洲国产影院| 在线精品视频一区二区三四| 亚洲一区二区精品| 日韩一区二区精品| 欧美一区成人| 欧美激情亚洲激情| 久热成人在线视频| 国产精品女主播| 亚洲激情图片小说视频| 伊人久久噜噜噜躁狠狠躁| 亚洲夜间福利| 一区二区三区日韩在线观看| 免费视频一区| 欧美成人四级电影| 一区二区自拍| 欧美一区91| 欧美一级电影久久| 国产精品色网| 亚洲天堂成人| 亚洲欧美日韩国产精品| 欧美女主播在线| 91久久国产自产拍夜夜嗨| 亚洲第一视频| 美女精品国产| 欧美激情一区二区三区在线| 亚洲成人在线视频播放| 欧美有码视频| 久久久久久久久久久一区 | 狠狠综合久久av一区二区老牛| 在线亚洲欧美| 亚洲午夜在线视频| 欧美日韩午夜剧场| 99热在这里有精品免费| 亚洲婷婷在线| 国产精品家教| 欧美亚洲视频在线观看| 久久人人爽爽爽人久久久| 影音先锋国产精品| 蜜桃久久av| 亚洲理伦在线| 亚洲午夜女主播在线直播| 欧美亚韩一区| 欧美一区1区三区3区公司| 老司机aⅴ在线精品导航| 亚洲国产黄色片| 欧美精品激情在线观看| 一区二区三区 在线观看视频| 亚洲免费在线视频一区 二区| 国产精品无人区| 久久亚洲免费| 日韩亚洲视频在线| 欧美在线观看视频| 亚洲色无码播放| 亚洲欧美日韩专区| 欧美视频在线播放| 小黄鸭精品aⅴ导航网站入口 | 欧美一级专区| 伊人久久大香线| 欧美日产国产成人免费图片| 亚洲视频在线观看三级| 久热精品视频在线观看| 日韩视频在线观看| 国产精品入口66mio| 久久久久久穴| 99v久久综合狠狠综合久久| 久久av资源网| 亚洲美女中文字幕| 国产日韩精品综合网站| 你懂的国产精品永久在线| 亚洲视频中文| 亚洲成色www8888| 午夜精品久久久久久久久久久久久 | 亚洲在线观看视频网站| 免费日韩av| 欧美一级欧美一级在线播放| 亚洲国产精品一区二区尤物区 | 国产色综合天天综合网| 欧美成人有码| 欧美综合激情网| 一区二区三区精品国产| 欧美激情精品久久久久久久变态| 欧美一区二区在线播放| 日韩视频在线观看| 在线看视频不卡| 国产婷婷97碰碰久久人人蜜臀| 欧美日韩国产精品一区二区亚洲| 久久精品在线免费观看| 亚洲免费视频成人| 99热这里只有精品8| 91久久精品日日躁夜夜躁国产| 久久久久久亚洲综合影院红桃| 欧美一区二区三区视频| 亚洲欧美一区二区原创| 亚洲一区国产视频|