欧美三区_成人在线免费观看视频_欧美极品少妇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條評論
主站蜘蛛池模板: 久久丁香视频 | 好吊日在线视频 | 免费观看一区二区三区毛片 | 国产亚洲欧美在线 | 欧美一区精品 | 偿还的影视高清在线观看 | 日本免费大片免费视频 | 亚洲精品乱码久久久久久v 国产高清免费视频 | 日日摸夜夜骑 | 亚洲欧美第一页 | 日韩中文字幕不卡 | 精品欧美在线观看 | 成年人看的视频网站 | 奇米影视第四色av首页 | 精品视频免费观看 | 国产精品激情福利视频 | 欧美性猛交一区二区三区精品 | 欧美亚洲第一页 | 免费观看欧美一级高清 | 欧美八区| 色香婷婷| 亚洲精品国产福利在线观看 | 亚洲看| 久久亚洲综合 | 国产毛A片啊久久久久久A | 亚洲aⅴ | 欧美经典一区 | 久草这里只有精品 | 成人性爱视频在线观看 | 日本欧美一区二区三区不卡视频 | 成人精品在线观看 | 黄色网址入口 | 免费国产成人午夜在线观看 | 一区二区三区视频免费观看 | 色婷婷色综合缴情在线 | 亚洲精品第五页中文字幕 | 久久亚洲最大成人网4438 | 亚洲午夜精品一级在线 | 成人欧美在线观看免费视频 | 五月激情六月婷婷 | 欧美性色综合网 |