欧美三区_成人在线免费观看视频_欧美极品少妇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條評論
主站蜘蛛池模板: 无码免费人妻A片AAA毛 | 看免费大片 | 日韩精品免费 | 午夜国产亚洲精品一区 | 久久九九精品一区二区 | 久久久久久国产精品久久 | 日韩成人av在线 | 日韩精品一区二区三区不卡 | 欧美亚洲一区 | 国产色在线| 欧美视频三区 | 欧美国产日韩在线 | 精品无码国产一区二区日本 | 精品国产乱码久久久久久88av | 老色鬼久久AV综合亚洲健身 | 午夜国产亚洲精品一区 | 日韩在线第一 | 久久噜噜噜精品国产亚洲综合 | 在线欧美日韩 | 偷拍自拍视频在线观看 | 成人嫩草影院免费观看 | 国产精品二区三区 | 日韩精品网址 | 国产夜色 | 一区二区中文字幕 | 91久久久久久久久久久久久久 | 久久精品伊人 | 蜜臀视频在线播放 | 精品在线一区二区 | 黑色丝袜美女自安慰视频 | 国产精品成人免费一区久久羞羞 | jizz视频 | 色一欲一性一乱一区二区三区 | 26uuu.mobi | 欧美激情免费 | 亚洲精品成人 | 日本吻胸捏胸激烈床戏视频 | 亚洲最新色 | 奇米影视第四色在线 | 国产精品3区| 国产成人免费永久播放视频平台 |