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

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>
            欧美一区二区三区在线观看视频| 国产精品视区| 一本久久精品一区二区| 麻豆久久精品| 欧美福利视频网站| 亚洲电影在线观看| 久久国产乱子精品免费女| 性欧美video另类hd性玩具| 性欧美长视频| 久久伊人一区二区| 欧美激情性爽国产精品17p| 亚洲精品国产精品国自产观看 | 国产精品视频网址| 国产精品亚洲产品| 亚洲国产精品一区二区www在线 | 久久久免费精品视频| 免费一级欧美片在线播放| 欧美激情黄色片| 国产精品久久久久婷婷| 国产亚洲永久域名| 亚洲日本黄色| 性视频1819p久久| 亚洲国产欧美日韩精品| 亚洲综合精品| 久久综合成人精品亚洲另类欧美| 欧美日韩在线精品| 黄色日韩精品| 亚洲欧美日韩国产中文在线| 欧美成人午夜剧场免费观看| 欧美激情视频给我| 午夜精品婷婷| 国语精品一区| 一区二区三区免费网站| 午夜在线a亚洲v天堂网2018| 久久久91精品国产| 亚洲国产裸拍裸体视频在线观看乱了中文 | 欧美日韩一区二区三区免费看 | 国产精品看片资源| 亚洲国产成人不卡| 欧美在线中文字幕| 亚洲美女视频在线观看| 久久一区二区三区四区| 欧美婷婷六月丁香综合色| 在线观看精品| 久久野战av| 亚洲欧美综合国产精品一区| 欧美日韩亚洲成人| 欧美一区二区在线看| 欧美日韩你懂的| 免费日韩一区二区| 国内精品视频久久| 午夜亚洲激情| 9l国产精品久久久久麻豆| 欧美成人精品一区二区| 激情亚洲一区二区三区四区| 久久久7777| 香蕉av777xxx色综合一区| 国产精品久久久久久久7电影| 日韩视频三区| 亚洲精品国产精品乱码不99按摩| 久久亚洲国产精品一区二区| 尤物视频一区二区| 麻豆国产精品一区二区三区| 久久精品系列| 亚洲高清久久| 欧美激情中文字幕乱码免费| 浪潮色综合久久天堂| 在线观看亚洲精品| 欧美国产精品| 免费av成人在线| 亚洲精品美女在线| 亚洲三级毛片| 欧美日韩在线观看一区二区| 亚洲欧美日韩在线| 亚洲欧美日韩一区二区三区在线| 国产女人水真多18毛片18精品视频| 午夜精品一区二区三区在线播放| 午夜精品99久久免费| 狠狠色狠狠色综合系列| 女人香蕉久久**毛片精品| 模特精品裸拍一区| 一区二区三区高清| 亚洲午夜电影在线观看| 国语自产精品视频在线看一大j8| 欧美成人精品福利| 欧美日韩精品久久久| 欧美在线观看网址综合| 久久精品视频网| 一本色道久久加勒比88综合| 亚洲午夜一二三区视频| 国产在线欧美日韩| 91久久久久久久久久久久久| 国产精品青草久久久久福利99| 久久免费视频在线观看| 欧美日韩国产一区| 久久综合久久综合九色| 欧美日韩mv| 另类亚洲自拍| 欧美午夜不卡| 亚洲福利视频网| 国产欧美日韩亚洲一区二区三区| 欧美国产高潮xxxx1819| 国产精品视频一区二区高潮| 欧美成人精品| 国产一区二区丝袜高跟鞋图片| 欧美激情一区在线| 国产一区二区三区久久 | 午夜精品视频一区| 亚洲黄色精品| 亚洲深爱激情| 亚洲九九九在线观看| 性欧美1819性猛交| 亚洲在线视频网站| 欧美高清一区| 久久综合网hezyo| 欧美高清影院| 国产乱码精品一区二区三区不卡| 亚洲日本va午夜在线电影| 一区二区在线视频观看| 亚洲成色www久久网站| 国产精品自拍网站| 亚洲激情国产精品| 激情六月婷婷久久| 亚洲欧美日韩区| 亚洲一区二区三区四区视频| 欧美+日本+国产+在线a∨观看| 欧美一区免费视频| 国产精品红桃| 亚洲深夜福利| 亚洲在线视频网站| 欧美日韩一区三区四区| 亚洲日本精品国产第一区| 亚洲国产成人高清精品| 久久国产一区二区| 性欧美18~19sex高清播放| 欧美午夜激情在线| 一区二区三区四区五区在线| 国产精品99久久久久久久久| 欧美日韩成人| 99riav1国产精品视频| 一区二区三区蜜桃网| 欧美日韩一区二区视频在线| 艳女tv在线观看国产一区| 亚洲一区二区高清| 国产精品女人网站| 亚洲免费在线视频| 久久久天天操| 亚洲国产精品久久人人爱蜜臀| 另类图片国产| 亚洲精品美女在线| 亚洲综合社区| 国产偷久久久精品专区| 久久精品91| 欧美激情视频一区二区三区免费| 狠狠久久综合婷婷不卡| 久久免费少妇高潮久久精品99| 欧美电影免费网站| 亚洲视频视频在线| 国产伦精品一区二区三区高清| 欧美一区二区三区在| 久久综合九色综合欧美狠狠| 亚洲国产另类久久精品| 嫩草国产精品入口| 99ri日韩精品视频| 久久影音先锋| 一区二区三区精品久久久| 国产精品久久99| 久久色中文字幕| 亚洲免费不卡| 久久免费视频在线观看| 亚洲精品小视频在线观看| 国产精品一区2区| 免播放器亚洲| 欧美亚洲在线观看| 亚洲青涩在线| 久久综合九色综合久99| 亚洲一二三级电影| 红桃视频亚洲| 国产精品对白刺激久久久| 久久精品国产99国产精品澳门| 亚洲精品日产精品乱码不卡| 久久裸体视频| 香蕉乱码成人久久天堂爱免费| 影音先锋中文字幕一区二区| 欧美理论在线播放| 久久精品亚洲一区二区| 中文亚洲免费| 91久久综合| 国产一区二区精品久久| 欧美日韩性生活视频| 久久在线91| 欧美一区二区高清| 99精品久久久| 亚洲国产99| 久久本道综合色狠狠五月| 亚洲午夜精品国产| 亚洲另类在线一区| 亚洲大胆av| 在线播放豆国产99亚洲| 国产区在线观看成人精品|