欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

C++中的explicit

系統 2466 0

簡而言之: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.

-------------

#include < iostream >
using namespace std;

class String
... {
private :
int size;
int capacity;
char * buff;
public :
String()
... {}
String(
int size) ... {
cout
<< " int " << endl;
}
// constructorandimplicitconversionoperator
String( const char * ) ... {} // constructorandimplicitconversionoperator
~ String() ... {}
}
;

int main()
... {
Strings
= " hello " ; // OK,convertaC-StringintoaStringobject
int ns = 0 ;
s
= 1 ; // 1oops,programmerintendedtowritens=1,
// 龍思:callconstrutor,thencallassignmentoperaterfunction?
return 0 ;
}

C++中的explicit


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久一| 一区二区三区在线电影 | 性色网址 | 欧美伦理大片 | 午夜视频直播 | 久久天天 | 精品久久国产 | 日本小网站 | 91免费精品国偷自产在线在线 | 国产专区在线播放 | 亚洲免费在线观看视频 | 国产精品免费av | 亚洲国产二区 | 91亚洲精品在线观看 | 日本午夜在线 | 欧美日韩在线国产 | 中文字幕成人乱码在线电影 | 国产欧美日韩第一页 | 久久久久久久久成人 | 全黄裸片武则天一级第4季 亚洲一区国产 | 日日干夜夜干 | 成人免费毛片aaaaaa片 | 男女真实有遮挡xx00动态图 | 天天爽天天色 | 欧美国产精品一区二区 | 欧美午夜久久 | 国产喷水 | 福利影院在线看 | 欧美韩国日本一区 | 国产亚洲欧美另类第一页 | 黄网站涩免费蜜桃网站 | 成人亚洲网站 | 国产精品一区二区在线观看 | 99热精品在线 | 中国一级免费视频 | 亚洲视频毛片 | 一级做a爰性色毛片免费 | 极品丝袜高跟91极品系列 | 波多野结在线 | 日韩免费一区二区 | 国产亚洲精品久久无亚洲 |