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

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>
            欧美高清在线视频| 久久精品欧洲| 亚洲系列中文字幕| 久久久久久久一区二区| 国产精品免费在线 | 国内精品久久久久久| 亚洲午夜电影网| 亚洲国产日韩在线一区模特| 亚洲一区亚洲| 欧美午夜激情小视频| 中文在线不卡| 一本久久知道综合久久| 欧美看片网站| 一区二区三区四区蜜桃| 日韩午夜在线播放| 欧美日韩精品在线| 亚洲一区二区三区涩| 亚洲午夜视频在线观看| 国产精品一区久久| 久久男女视频| 久久躁日日躁aaaaxxxx| 亚洲国产另类精品专区 | 香蕉久久久久久久av网站| 国产精品第十页| 欧美在线观看你懂的| 欧美在线视频免费观看| 国内揄拍国内精品久久| 久久久久久97三级| 噜噜噜久久亚洲精品国产品小说| 亚洲国产精品久久久久婷婷884 | 欧美成人黄色小视频| 亚洲精品国产视频| 99re国产精品| 国产亚洲成精品久久| 麻豆av一区二区三区| 免费亚洲一区| 亚洲一区综合| 久久精品理论片| 亚洲国产老妈| 一区二区三区四区国产精品| 国产日韩一区二区三区| 免费永久网站黄欧美| 亚洲一区二区三区中文字幕在线| 99精品免费| 国产日韩欧美亚洲一区| 米奇777在线欧美播放| 欧美国产一区二区三区激情无套| 一区二区三区 在线观看视| 香蕉精品999视频一区二区| 91久久精品一区| 亚洲欧美激情视频在线观看一区二区三区| 国产专区精品视频| 日韩视频在线免费| 国产在线不卡| 亚洲精品综合在线| 精品动漫3d一区二区三区免费版 | 在线观看视频免费一区二区三区| 最新69国产成人精品视频免费| 国产精品日韩专区| 亚洲电影av在线| 国产欧美精品一区二区三区介绍| 免费在线亚洲欧美| 国产精品国产三级国产普通话三级| 久久久久在线观看| 国产精品大片wwwwww| 亚洲国产福利在线| 激情欧美国产欧美| 亚洲综合色视频| 亚洲视频在线观看视频| 久久蜜桃精品| 亚洲欧美在线视频观看| 欧美成年人视频网站| 久久久久久亚洲综合影院红桃| 欧美视频一区二区三区在线观看| 美女精品在线| 国产在线一区二区三区四区| 一区二区免费在线观看| 99精品欧美一区二区三区综合在线 | 亚洲视频中文| 欧美福利视频在线| 久久精品欧洲| 国产女主播一区| 亚洲免费一在线| 亚洲免费中文| 欧美三级网址| 一区二区精品| 亚洲最新在线| 欧美日韩视频| 在线亚洲高清视频| 亚洲欧美国产日韩天堂区| 欧美日韩在线一区二区| 亚洲精品无人区| 亚洲神马久久| 国产精品久久亚洲7777| 亚洲福利国产精品| 久久久久久久久综合| 蜜臀av性久久久久蜜臀aⅴ四虎| 狠久久av成人天堂| 久久久久一区二区三区| 久久综合给合| 亚洲国产一区二区三区在线播 | 久久人人97超碰国产公开结果 | 久久只精品国产| 免费在线一区二区| 亚洲韩日在线| 麻豆成人av| 亚洲人成高清| 亚洲——在线| 国产欧美午夜| 久久综合给合| 欧美成年人视频| 亚洲精品午夜| 国产精品久久国产精品99gif| 亚洲一区二区动漫| 欧美在线观看一区二区| 在线观看三级视频欧美| 欧美精品日韩综合在线| 亚洲一级影院| 欧美成人午夜影院| 亚洲一二三四久久| 韩国av一区二区| 欧美精品aa| 亚洲永久视频| 狂野欧美一区| 亚洲视频在线免费观看| 国产一区二区三区精品欧美日韩一区二区三区 | 欧美一级理论片| 国内伊人久久久久久网站视频 | 亚洲图片欧洲图片日韩av| 国产精品免费看| 久久精品亚洲精品国产欧美kt∨| 欧美成人午夜免费视在线看片 | 亚洲最黄网站| 久久久亚洲人| 一区二区三区四区国产| 国内精品视频一区| 欧美视频日韩视频在线观看| 久久精品日韩| 在线一区二区三区四区五区| 欧美专区日韩视频| 一本久道久久综合狠狠爱| 黄色日韩网站| 国产精品国码视频| 欧美成人在线免费视频| 亚洲欧美三级伦理| 欧美国产在线观看| 久久国产手机看片| 日韩一级精品视频在线观看| 国产日韩欧美91| 欧美日韩日本网| 免费在线看成人av| 欧美一区二区福利在线| 中文久久精品| 亚洲国产精品va在线看黑人动漫| 久久久国产91| 国产日韩欧美电影在线观看| 欧美黄网免费在线观看| 久久久久久97三级| 欧美一区二区视频在线观看2020| 一本到12不卡视频在线dvd| 欧美成人一区二区三区| 久久久久久9| 久久精品亚洲国产奇米99| 在线天堂一区av电影| 亚洲第一精品影视| 国产亚洲欧美中文| 国产女主播一区| 国产伦精品一区二区三区在线观看| 欧美精品一区视频| 欧美激情中文字幕一区二区| 久久综合九色| 久久亚洲国产成人| 久久婷婷久久| 久久精品国产精品亚洲| 欧美综合激情网| 欧美影院在线| 欧美亚洲综合在线| 香蕉久久夜色精品国产| 国产一区二区久久| 国产一区二区精品久久99| 国产欧美视频一区二区| 国内精品美女在线观看| 激情综合在线| 亚洲国产成人精品女人久久久 | 久久超碰97人人做人人爱| 欧美一区二区三区视频免费播放 | 久久视频免费观看| 欧美中文字幕精品| 久久久人成影片一区二区三区观看| 久久国产毛片| 欧美激情麻豆| 99视频在线观看一区三区| 夜夜嗨av一区二区三区中文字幕 | 国产午夜精品全部视频播放| 国产视频精品va久久久久久| 国产自产在线视频一区| 国内精品视频在线播放| 亚洲人成网站在线播| 亚洲午夜在线| 久久久久久欧美|