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

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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

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

這個(gè) 《ANSI/ISO C++ Professional Programmer's Handbook 》是這樣說(shuō)的

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ù)  更多評(píng)論   

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

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

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ù)  更多評(píng)論   

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

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

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

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

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

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

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>
            亚洲国产mv| 欧美午夜一区| 亚洲日本中文字幕区| 久久久www| 久久久精品国产一区二区三区| 国产视频不卡| 国产一区二区三区四区五区美女 | 亚洲欧美成人网| 亚洲视频在线看| 欧美中文字幕在线视频| 蜜臀91精品一区二区三区| 欧美大片在线观看| 国产精品亚洲а∨天堂免在线| 国内精品国语自产拍在线观看| 亚洲国产成人精品女人久久久 | 国产精品xvideos88| 国产欧美亚洲视频| 尤物九九久久国产精品的分类| 亚洲另类自拍| 欧美一级专区免费大片| 欧美第一黄色网| 亚洲亚洲精品三区日韩精品在线视频| 久久国产精品免费一区| 欧美激情久久久| 国产精品久久久久久久久免费樱桃 | 久久久久在线观看| 欧美成人综合一区| 夜夜爽www精品| 久久蜜桃香蕉精品一区二区三区| 欧美激情精品久久久久久免费印度 | 欧美日韩在线一区| 韩国美女久久| 亚洲在线免费视频| 欧美高清视频在线播放| 亚洲综合精品一区二区| 欧美高清在线视频| 尤物精品国产第一福利三区 | 亚洲国产视频一区| 亚洲欧美日本日韩| 欧美日韩另类视频| 91久久精品国产91久久| 久久久国产精彩视频美女艺术照福利| 亚洲乱码一区二区| 久热成人在线视频| 国内精品伊人久久久久av影院 | 农村妇女精品| 亚洲欧美日韩在线综合| 欧美日韩一区二区三区视频| 亚洲激情精品| 欧美国产一区二区三区激情无套| 欧美在线视频不卡| 亚洲毛片视频| 欧美福利视频网站| 亚洲第一视频网站| 久久精品视频亚洲| 午夜在线精品| 国产一区二区丝袜高跟鞋图片| 午夜久久黄色| 亚洲欧美日韩一区| 国产区亚洲区欧美区| 欧美一区二区三区免费视频| 一区二区日韩欧美| 国产精品羞羞答答| 久久精品国产亚洲精品| 欧美一区二区三区在线| 国产主播精品| 欧美激情bt| 欧美激情一区三区| 亚洲午夜久久久久久久久电影院| 夜夜爽99久久国产综合精品女不卡| 欧美涩涩视频| 欧美在线一二三| 久久国产精品久久久久久| 午夜在线播放视频欧美| 亚洲二区在线| 久久国产免费| 欧美午夜精品久久久| 亚洲欧美日本日韩| 欧美一区二区视频免费观看| 国内在线观看一区二区三区| 午夜亚洲激情| 欧美r片在线| 欧美少妇一区二区| 久久精品综合网| 久久国内精品视频| 久久精品综合一区| 亚洲欧美日韩天堂| 这里只有精品丝袜| 亚洲欧美日韩另类精品一区二区三区| 亚洲日韩欧美一区二区在线| 亚洲一区二区三区精品视频| 亚洲免费精品| 国产一区二区黄色| 亚洲第一级黄色片| 国产在线麻豆精品观看| 欧美视频在线观看视频极品| 国产精品中文在线| 蜜臀99久久精品久久久久久软件| 欧美aa国产视频| 亚洲欧美制服中文字幕| 久久久久久精| 亚洲午夜精品一区二区三区他趣| 午夜精品视频在线| 艳妇臀荡乳欲伦亚洲一区| 欧美在线播放高清精品| 日韩一区二区久久| 欧美一区二区三区精品| 日韩亚洲精品视频| 久久手机免费观看| 久久激情网站| 国产精品一区二区在线观看| 亚洲激情不卡| 激情自拍一区| 亚洲男女自偷自拍| 一区二区三区视频在线看| 亚洲另类自拍| 久久夜色撩人精品| 久久成人免费| 国产精品久久久久永久免费观看| 欧美好吊妞视频| 好吊色欧美一区二区三区视频| 一区二区三区日韩在线观看| 日韩午夜在线电影| 欧美大片va欧美在线播放| 久久午夜精品一区二区| 国产女人水真多18毛片18精品视频| 亚洲精品美女在线观看| 亚洲国产精品一区二区尤物区| 欧美专区亚洲专区| 久久久久国产精品人| 一区二区三区免费观看| 99成人在线| 欧美激情一区二区三区在线视频| 亚洲电影专区| 亚洲区中文字幕| 欧美高清一区| 亚洲精品一区二区三区婷婷月| 亚洲日本激情| 欧美精品导航| 亚洲乱码国产乱码精品精98午夜 | 亚洲图片欧美午夜| 亚洲综合丁香| 国产伦精品一区二区三区视频黑人| 亚洲素人一区二区| 欧美在线你懂的| 国产一区二区三区观看| 欧美专区18| 欧美成人日韩| 日韩网站免费观看| 欧美成人国产| 一区二区高清在线| 久久国产精品久久久| 国产真实乱偷精品视频免| 久久综合九色综合网站| 欧美国产一区二区| 亚洲四色影视在线观看| 国产精品一区二区视频| 久久国产一区| 欧美不卡视频一区发布| 9人人澡人人爽人人精品| 国产精品亚洲欧美| 老司机精品福利视频| 亚洲精品在线观| 欧美主播一区二区三区美女 久久精品人 | 麻豆视频一区二区| 亚洲国产日韩欧美在线99| 欧美成人黑人xx视频免费观看| 亚洲精品色婷婷福利天堂| 午夜精品久久久久久久99樱桃| 国产亚洲欧美一区二区三区| 麻豆免费精品视频| 亚洲毛片视频| 老司机免费视频一区二区三区 | 美国十次成人| 中国女人久久久| 国产视频精品va久久久久久| 老司机67194精品线观看| av成人老司机| 久久女同精品一区二区| 亚洲视频欧美视频| 国产婷婷一区二区| 欧美日韩高清在线播放| 亚欧美中日韩视频| 亚洲激情视频在线观看| 久久精品国产欧美亚洲人人爽| 一区二区三区视频在线观看| 在线观看国产日韩| 国产麻豆精品在线观看| 欧美精品在线观看| 久久久久久夜精品精品免费| 亚洲一二三四区| 亚洲激精日韩激精欧美精品| 亚洲美女黄色| 91久久精品国产91久久| 午夜综合激情| 夜色激情一区二区| 亚洲国产精品va| 国内精品视频久久| 国产精品99久久久久久www| 亚洲第一精品久久忘忧草社区|