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

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>
            亚洲欧美日韩精品综合在线观看| 国语自产精品视频在线看一大j8| 狠狠色丁香婷婷综合久久片| 欧美性生交xxxxx久久久| 欧美在线观看一区二区三区| 久久久久久久久久久成人| 久久精品国产2020观看福利| 久久久国产成人精品| 亚洲欧美怡红院| 亚洲免费伊人电影在线观看av| 亚洲一区二区av电影| 亚洲欧美成人| 亚洲天堂激情| 欧美在线观看你懂的| 欧美大片一区二区三区| 欧美高清在线精品一区| 在线亚洲激情| 欧美激情视频在线播放| 欧美亚韩一区| 一区二区免费在线视频| 欧美va亚洲va日韩∨a综合色| 亚洲图片在线观看| 免费亚洲一区二区| 一区精品久久| 久久精品99国产精品酒店日本| 亚洲激情不卡| 亚洲综合欧美日韩| 欧美无砖砖区免费| 日韩一级成人av| 欧美激情va永久在线播放| 久久久久久久综合色一本| 国产视频在线观看一区二区| 亚洲欧美资源在线| 中文精品99久久国产香蕉| 国产精品草莓在线免费观看| 亚洲午夜精品在线| 亚洲欧美日韩在线一区| 国产美女精品视频免费观看| 亚洲在线观看视频| 亚洲综合社区| 亚洲国产成人一区| 亚洲精品欧美极品| 国产精品久久久久婷婷| 亚洲综合日韩在线| 先锋影音国产一区| 在线免费不卡视频| 亚洲国产经典视频| 亚洲小视频在线| 国产精品亚洲不卡a| 久久亚洲综合| 国产精品成人久久久久| 久久福利影视| 国产精品www994| 免费久久99精品国产自| 欧美私人网站| 亚洲国产影院| 在线不卡视频| 久久精品视频在线播放| 午夜精品久久久| 欧美激情第一页xxx| 免费试看一区| 国产精品久久久久久久久久免费 | 亚洲欧美日韩精品在线| 欧美一区二区在线播放| 99视频+国产日韩欧美| 久久精品免费观看| 香蕉乱码成人久久天堂爱免费| 欧美日韩1区2区3区| 欧美激情小视频| 亚洲日本视频| 亚洲高清二区| 亚洲人在线视频| 欧美国产国产综合| 亚洲裸体视频| 午夜视频一区在线观看| 一区二区三区你懂的| 欧美1级日本1级| 欧美激情精品久久久久久蜜臀| 精品福利免费观看| 乱中年女人伦av一区二区| 国产精品男人爽免费视频1| 中文成人激情娱乐网| 久久精品色图| 亚洲人人精品| 国产精品视频999| 久久综合九九| 亚洲天堂久久| 你懂的国产精品永久在线| 国产午夜精品理论片a级大结局| 亚洲男同1069视频| 亚洲国产高潮在线观看| 欧美在线播放| 一本色道久久综合精品竹菊 | 欧美国产日韩在线| 亚洲视频你懂的| 一区在线免费观看| 国产欧美精品日韩| 欧美电影免费观看高清| 在线亚洲欧美专区二区| 亚洲人被黑人高潮完整版| 新67194成人永久网站| 99国产麻豆精品| 亚洲国产天堂久久综合网| 国产欧美 在线欧美| 欧美日韩在线免费观看| 裸体丰满少妇做受久久99精品| 亚洲视频在线观看一区| 欧美日韩国产综合视频在线观看 | 一级成人国产| 欧美国产综合视频| 亚洲福利视频一区二区| 久久综合亚州| 亚洲激情视频在线| 亚洲乱码国产乱码精品精| 国产婷婷一区二区| 国产精品一二一区| 亚洲色图在线视频| 一区二区三区欧美成人| 正在播放欧美一区| 欧美在线国产| 欧美精品激情在线观看| 欧美日韩精品一本二本三本| 欧美日韩在线播放一区| 国产日产亚洲精品| 亚洲日本国产| 午夜亚洲福利在线老司机| 久久久国产亚洲精品| 欧美黑人在线观看| 一区二区三区日韩精品视频| 欧美永久精品| 欧美日韩在线不卡| 在线看片欧美| 久久久久国产精品厨房| 日韩一区二区免费看| 久久久噜噜噜久久人人看| 欧美亚男人的天堂| 亚洲人成亚洲人成在线观看图片| 亚洲午夜在线视频| 91久久视频| 久色婷婷小香蕉久久| 国产精品美女久久久久久2018| 亚洲激情一区二区| 嫩草成人www欧美| 久久久久久国产精品mv| 国产伪娘ts一区| 欧美在线免费观看视频| 亚洲免费一级电影| 国产欧美精品在线播放| 亚洲一区二区网站| 一区二区三区国产精品| 欧美日韩国产精品一区| 99pao成人国产永久免费视频| 免费国产一区二区| 久久免费视频这里只有精品| 狠狠色丁香婷婷综合久久片| 久久免费国产| 麻豆精品传媒视频| 亚洲毛片一区二区| 在线一区二区三区做爰视频网站| 亚洲高清视频中文字幕| 免费91麻豆精品国产自产在线观看| 亚洲国产导航| 日韩视频免费观看| 国产欧美一区二区白浆黑人| 久久久国产午夜精品| 久久午夜电影| 亚洲中字黄色| 欧美成人午夜免费视在线看片| 日韩亚洲视频在线| 久久成人18免费网站| 日韩视频―中文字幕| 欧美一区二区黄色| 99爱精品视频| 久久久久国产精品www| 亚洲午夜精品一区二区| 久久综合婷婷| 久久国产精品72免费观看| 欧美激情1区2区| 美腿丝袜亚洲色图| 国产日韩欧美三区| 中文亚洲字幕| 99re6热在线精品视频播放速度| 亚洲香蕉视频| 亚洲欧美综合另类中字| 欧美日本在线播放| 91久久精品国产| 激情文学综合丁香| 亚洲免费综合| 久久国产精品久久久久久| 欧美体内she精视频在线观看| 欧美成在线观看| 亚洲精品日产精品乱码不卡| 久久最新视频| 亚洲激情在线播放| 亚洲神马久久| 国产色产综合产在线视频| 午夜国产精品视频| 久久综合久久综合九色| 亚洲国产精品综合| 欧美日韩三级|