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

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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

關于C++的 explicit關鍵字

Posted on 2006-02-05 05:16 Tommy Liang 閱讀(10035) 評論(6)  編輯 收藏 引用 所屬分類: C++語言與規范

簡而言之:explicit修飾的構造函數不能擔任轉換函數

這個 《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: 關于C++的 explicit關鍵字  回復  更多評論   

2006-04-18 10:45 by youtr
呵呵,寫的不錯

# re: 關于C++的 explicit關鍵字  回復  更多評論   

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: 關于C++的 explicit關鍵字  回復  更多評論   

2008-06-20 11:13 by Anecdote
學習了。謝謝

# re: 關于C++的 explicit關鍵字[未登錄]  回復  更多評論   

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

# re: 關于C++的 explicit關鍵字  回復  更多評論   

2009-03-16 10:35 by inzer
不錯

# re: 關于C++的 explicit關鍵字  回復  更多評論   

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>
            最新国产成人在线观看| 影音先锋中文字幕一区| 欧美日韩在线播放| 一本久久青青| 欧美va天堂va视频va在线| 国产精品色午夜在线观看| 精品福利电影| 久久嫩草精品久久久精品| 久久免费视频观看| 欧美激情视频在线免费观看 欧美视频免费一 | 久久久久亚洲综合| 欧美日韩ab片| 一级成人国产| 亚洲国产aⅴ天堂久久| 欧美在线啊v| 99亚洲一区二区| 免费观看30秒视频久久| 亚洲精品日韩一| 欧美国产日韩视频| 国产精品久久久免费| 中文无字幕一区二区三区| 亚洲大胆人体视频| 国产精品成人免费| 亚洲天堂av在线免费| 中文国产一区| 久久国产精品一区二区三区| 在线看片成人| 久久国产福利| 亚洲欧美久久久久一区二区三区| 亚洲校园激情| 亚洲日韩欧美视频一区| 亚洲一区美女视频在线观看免费| 欧美日韩综合在线免费观看| 亚洲欧洲日产国产网站| 国产精品永久入口久久久| 久久免费一区| 欧美成人小视频| 亚洲理论电影网| 亚洲国产成人高清精品| 韩国在线视频一区| 亚洲婷婷国产精品电影人久久| 国产毛片精品国产一区二区三区| 久久成人免费| 欧美激情1区2区3区| 亚洲欧洲综合另类| 国产欧美视频一区二区| 欧美诱惑福利视频| 亚洲国产一二三| 国产一区二区三区在线播放免费观看 | 美女爽到呻吟久久久久| 欧美日韩日本网| 欧美14一18处毛片| 国产一区在线视频| 亚洲在线网站| 欧美一区二区三区电影在线观看| 欧美久久99| 免费观看亚洲视频大全| 欧美日韩亚洲精品内裤| 欧美综合国产精品久久丁香| 羞羞色国产精品| 国产亚洲综合在线| 亚洲伊人观看| 亚洲欧美国产视频| 日韩亚洲欧美在线观看| 久久人人爽人人爽爽久久| 韩国美女久久| 久久中文字幕一区| 午夜精品久久久久久久久久久久久 | 久久精品一区二区| 欧美日本网站| 午夜一级在线看亚洲| 日韩一级精品视频在线观看| 久久久亚洲人| 99精品久久久| 国产日韩欧美中文| 欧美日韩1080p| 快播亚洲色图| 亚洲欧美国产不卡| 99精品国产福利在线观看免费| 欧美在线看片| 麻豆精品在线观看| 欧美高清在线视频| 久久久久久久性| 亚洲欧美伊人| 亚洲网站视频福利| 国产日韩精品在线| 国产亚洲成av人片在线观看桃| 欧美视频成人| 欧美午夜激情小视频| 欧美国产日韩亚洲一区| 国产精品―色哟哟| 欧美日韩在线大尺度| 欧美日韩在线视频首页| 国产精品萝li| 亚洲欧美另类久久久精品2019| 欧美成人免费一级人片100| 欧美99在线视频观看| 亚洲国产日韩欧美在线99| 亚洲人午夜精品| 亚洲欧美日韩精品在线| 久久综合一区二区| 国产精品日本精品| 亚洲精品中文字幕在线| 久久国产欧美精品| 亚洲国产经典视频| 欧美一区二区三区四区高清 | 猛干欧美女孩| 中国av一区| 欧美精品在线观看播放| 国产欧美一区二区三区在线看蜜臀 | 欧美日韩午夜剧场| 一区久久精品| 久久精品99久久香蕉国产色戒| 欧美xx69| 香蕉尹人综合在线观看| 亚洲国产一区二区三区a毛片| 亚洲一区观看| 国产精品免费区二区三区观看| 亚洲国产一区二区三区青草影视 | 韩国免费一区| 性伦欧美刺激片在线观看| 欧美影院视频| 免费成人高清视频| 欧美一区二区在线看| 欧美激情亚洲国产| 久久人人爽爽爽人久久久| 欧美国产日本高清在线| 久久久一区二区| 国产精品免费观看在线| 亚洲激情视频网站| 一区二区在线观看视频在线观看 | 国产日韩欧美综合| 一本色道久久| 亚洲色在线视频| 亚洲午夜视频在线| 日韩一级免费| 伊人狠狠色j香婷婷综合| 欧美成人免费小视频| 欧美日韩一级片在线观看| 欧美一级欧美一级在线播放| 一区二区三区成人| 在线日韩日本国产亚洲| 夜夜爽夜夜爽精品视频| 在线观看国产日韩| 国产精品99久久不卡二区| 亚洲国产一区二区视频| 亚洲主播在线播放| 亚洲无线一线二线三线区别av| 欧美综合77777色婷婷| 久久久噜噜噜| 亚洲小说欧美另类婷婷| 欧美日韩国产成人在线免费| 毛片一区二区三区| 国产一区二区毛片| 亚洲免费一在线| 久久精品视频在线看| 亚洲欧美日韩高清| 中国成人黄色视屏| 国产日韩视频一区二区三区| 欧美成人免费播放| 香蕉成人伊视频在线观看| 国产一区日韩一区| 欧美精品一区二区三区高清aⅴ| 国内精品美女在线观看| 夜夜爽www精品| av不卡在线观看| 国产精品九九久久久久久久| 欧美一区二区三区喷汁尤物| 亚洲美女尤物影院| 国产精品色午夜在线观看| 一本在线高清不卡dvd| 欧美欧美在线| 亚洲综合999| 欧美国产欧美亚洲国产日韩mv天天看完整 | 亚洲综合精品自拍| 久久久精品国产免费观看同学| 国产一区二区黄色| 欧美国产日韩视频| 亚洲视频免费在线观看| 久久精品二区| 亚洲精品久久嫩草网站秘色| 欧美日韩精品在线视频| 亚洲欧美99| 亚洲人成7777| 欧美专区18| 亚洲作爱视频| 永久免费精品影视网站| 欧美午夜电影完整版| 裸体女人亚洲精品一区| 亚洲美女视频| 欧美国产乱视频| 亚欧美中日韩视频| 亚洲视频一区二区免费在线观看| 国产欧美精品在线| 欧美精品久久久久久久久久| 午夜亚洲伦理| 亚洲专区在线视频| 一区二区三区成人精品| 91久久国产综合久久蜜月精品 | 欧美在线观看一二区|