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

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>
            久久裸体艺术| 欧美性猛交视频| 亚洲三级观看| 久久网站免费| 麻豆精品网站| 欧美黄色片免费观看| 亚洲激情电影中文字幕| 99在线热播精品免费99热| 一区二区激情视频| 久久精品综合一区| 中文亚洲视频在线| 午夜电影亚洲| 欧美在线视频观看| 欧美激情在线免费观看| 亚洲精品在线电影| 午夜一区二区三区在线观看| 久久久久国产精品厨房| 欧美日本韩国一区二区三区| 国产精品美女999| 影音先锋中文字幕一区二区| 亚洲美女性视频| 欧美一区二区黄| 欧美顶级少妇做爰| 亚洲一区二区综合| 欧美成人精品一区二区| 国产精品自在欧美一区| 亚洲国产婷婷| 小处雏高清一区二区三区| 欧美成人激情视频| 亚洲国产色一区| 精品成人在线| 日韩亚洲在线观看| 久久av一区二区三区漫画| 欧美第一黄色网| 国产精品99久久不卡二区| 久久精品国产久精国产思思| 欧美片在线播放| 在线看视频不卡| 欧美在线看片a免费观看| 亚洲第一主播视频| 欧美尤物巨大精品爽| 欧美日韩视频在线第一区| 欧美在线视频日韩| 欧美日韩国产欧| 亚洲国产美女精品久久久久∴| 欧美在线视频一区二区| 亚洲精品少妇30p| 美女免费视频一区| 精品成人在线视频| 久久这里只有| 欧美专区在线| 国产综合久久| 久久国产精品一区二区| 亚洲一区二区三区成人在线视频精品| 欧美另类极品videosbest最新版本| 亚洲国产小视频在线观看| 免费看av成人| 老司机67194精品线观看| 伊人蜜桃色噜噜激情综合| 久久精品九九| 欧美在线视频全部完| 91久久极品少妇xxxxⅹ软件| 久久久久免费| 久久精品国产一区二区三区免费看 | 美日韩精品免费| 久久影视三级福利片| 在线观看日韩av电影| 欧美mv日韩mv国产网站| 老司机午夜精品视频| 亚洲国产免费看| 亚洲国产专区校园欧美| 欧美激情亚洲激情| 亚洲一区二区三区免费在线观看| 中文一区二区| 国产综合网站| 亚洲动漫精品| 国产精品99一区二区| 久久国产精品久久久久久久久久| 欧美在线日韩精品| 91久久精品美女高潮| 亚洲免费av片| 国产深夜精品福利| 免费亚洲一区二区| 欧美日本免费一区二区三区| 亚洲欧美在线x视频| 久久久欧美精品| 一本色道久久综合亚洲精品按摩| 亚洲小说欧美另类婷婷| 精品电影一区| 一区二区91| 激情久久婷婷| 一本色道久久综合亚洲精品按摩 | 亚洲欧美在线另类| 伊人久久婷婷| 中文国产成人精品| 亚洲第一视频网站| 亚洲色图自拍| 亚洲丰满少妇videoshd| 一区二区激情视频| 亚洲国产欧美在线| 亚洲欧美日韩国产综合在线| 亚洲国产欧美在线人成| 亚洲欧美日韩系列| 一本久久综合亚洲鲁鲁五月天| 亚洲欧美精品一区| 日韩视频在线你懂得| 欧美综合77777色婷婷| 一区二区欧美在线观看| 久久久美女艺术照精彩视频福利播放| 亚洲香蕉视频| 欧美成人精品1314www| 久久久美女艺术照精彩视频福利播放| 欧美日韩免费网站| 欧美成人一区二区三区在线观看| 国产精品成人免费视频| 亚洲国产欧美不卡在线观看| 韩国女主播一区| 亚洲欧美综合精品久久成人| 欧美国产91| 亚洲电影视频在线| 欧美怡红院视频| 欧美一级二区| 国产精品xnxxcom| 亚洲另类在线一区| 亚洲三级毛片| 老鸭窝91久久精品色噜噜导演| 久久国产免费看| 国产欧美在线播放| 午夜视频在线观看一区二区三区| 亚洲综合三区| 欧美视频日韩| 亚洲最新在线视频| 亚洲一卡二卡三卡四卡五卡| 欧美日本一道本| 亚洲精品美女| 在线综合视频| 国产精品www色诱视频| 夜夜精品视频一区二区| 亚洲色图制服丝袜| 国产精品成人av性教育| 一区二区三区 在线观看视| 宅男精品导航| 久久综合色影院| 欧美性色视频在线| 亚洲精品一区二区三区在线观看 | 久久九九久久九九| 国产亚洲人成网站在线观看| 羞羞色国产精品| 久久人人97超碰人人澡爱香蕉| 国语自产在线不卡| 久久综合九色综合欧美狠狠| 亚洲国产成人精品久久| 一本色道久久综合亚洲精品不| 国产精品都在这里| 亚洲欧美国产精品桃花| 久久久久久久久岛国免费| 1024成人网色www| 欧美精品日韩三级| 一区二区三区日韩精品视频| 久久综合网hezyo| 亚洲精品小视频| 亚洲欧美成aⅴ人在线观看| 国产精品v亚洲精品v日韩精品| 亚洲自拍三区| 嫩草伊人久久精品少妇av杨幂| 亚洲国产天堂久久国产91| 欧美日韩八区| 欧美一区二区观看视频| 欧美肥婆bbw| 亚洲一区中文| 黄色免费成人| 欧美视频一区二区三区…| 午夜欧美不卡精品aaaaa| 欧美激情精品久久久久久免费印度| 一本色道久久综合| 韩日精品视频| 国产精品大片wwwwww| 久久久一区二区| 99成人免费视频| 美女日韩在线中文字幕| 亚洲午夜电影| 亚洲国产一区二区三区青草影视 | 亚洲丶国产丶欧美一区二区三区| 一级成人国产| 好男人免费精品视频| 欧美日韩日日夜夜| 久久黄色网页| 亚洲天堂网在线观看| 欧美成人午夜免费视在线看片| 亚洲欧美日韩国产中文在线| 亚洲欧洲视频| 国内精品久久久久久久影视蜜臀| 欧美日韩在线观看一区二区三区| 久久亚洲综合色一区二区三区| 亚洲一区网站| 一本到高清视频免费精品| 欧美好吊妞视频| 老司机亚洲精品| 久久精品99国产精品酒店日本| 在线亚洲精品|