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

來吧,朋友!

為C++瘋狂

[轉(zhuǎn)帖] c++中的explicit關鍵字

 c++中的explicit關鍵字用來修飾類的構造函數(shù),表明該構造函數(shù)是顯式的,既然有"顯式"那么必然就有"隱式",那么什么是顯示而什么又是隱式的呢?

如果c++類的構造函數(shù)有一個參數(shù),那么在編譯的時候就會有一個缺省的轉(zhuǎn)換操作:將該構造函數(shù)對應數(shù)據(jù)類型的數(shù)據(jù)轉(zhuǎn)換為該類對象,如下面所示:
class MyClass
{
public:
MyClass( int num );
}
....
MyClass obj = 10; //ok,convert int to MyClass
在上面的代碼中編譯器自動將整型轉(zhuǎn)換為MyClass類對象,實際上等同于下面的操作:
MyClass temp(10);
MyClass obj = temp;
上面的所有的操作即是所謂的"隱式轉(zhuǎn)換"。

如果要避免這種自動轉(zhuǎn)換的功能,我們該怎么做呢?嘿嘿這就是關鍵字explicit的作用了,將類的構造函數(shù)聲明為"顯示",也就是在聲明構造函數(shù)的時候前面添加上explicit即可,這樣就可以防止這種自動的轉(zhuǎn)換操作,如果我們修改上面的MyClass類的構造函數(shù)為顯示的,那么下面的代碼就不能夠編譯通過了,如下所示:
class MyClass
{
public:
explicit MyClass( int num );
}
....
MyClass obj = 10; //err,can't non-explict convert

class isbn_mismatch:public std::logic_error{
public:
explicit isbn_missmatch(const std::string &s):std:logic_error(s){}
isbn_mismatch(const std::string &s,const std::string &lhs,const std::string &rhs):
std::logic_error(s),left(lhs),right(rhs){}
const std::string left,right;
virtual ~isbn_mismatch() throw(){}
};


Sales_item& operator+(const Sales_item &lhs,const Sales_item rhs)
{
if(!lhs.same_isbn(rhs))
   throw isbn_mismatch("isbn missmatch",lhs.book(),rhs.book());
Sales_item ret(lhs);
ret+rhs;
return ret;
}


Sales_item item1,item2,sum;
while(cin>>item1>>item2)
{
try{
   sun=item1+item2;
}catch(const isbn_mismatch &e)
{
   cerr<<e.what()<<"left isbn is:"<<e.left<<"right isbn is:"<<e.right<<endl;
}
}

用于用戶自定義類型的構造函數(shù),指定它是默認的構造函數(shù),不可用于轉(zhuǎn)換構造函數(shù).因為構造函數(shù)有三種:1拷貝構造函數(shù)2轉(zhuǎn)換構造函數(shù)3一般的構造函數(shù)(我自己的術語^_^)
另:如果一個類或結(jié)構存在多個構造函數(shù)時,explicit 修飾的那個構造函數(shù)就是默認的

class isbn_mismatch:public std::logic_error{
public:
explicit isbn_missmatch(const std::string &s):std:logic_error(s){}
isbn_mismatch(const std::string &s,const std::string &lhs,const std::string &rhs):
std::logic_error(s),left(lhs),right(rhs){}
const std::string left,right;
virtual ~isbn_mismatch() throw(){}
};


Sales_item& operator+(const Sales_item &lhs,const Sales_item rhs)
{
if(!lhs.same_isbn(rhs))
   throw isbn_mismatch("isbn missmatch",lhs.book(),rhs.book());
Sales_item ret(lhs);
ret+rhs;
return ret;
}


Sales_item item1,item2,sum;
while(cin>>item1>>item2)
{
try{
   sun=item1+item2;
}catch(const isbn_mismatch &e)
{
   cerr<<e.what()<<"left isbn is:"<<e.left<<"right isbn is:"<<e.right<<endl;
}
}

 

這個 《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.

網(wǎng)上找的講的最好的貼:

C++ 中 explicit 關鍵字的作用
在 C++ 中, 如果一個類有只有一個參數(shù)的構造函數(shù),C++ 允許一種特殊的聲明類變量的方式。在這種情況下,可以直接將一個對應于構造函數(shù)參數(shù)類型的數(shù)據(jù)直接賦值給類變量,編譯器在編譯時會自動進行類型轉(zhuǎn)換,將對應于構造函數(shù)參數(shù)類型的數(shù)據(jù)轉(zhuǎn)換為類的對象。 如果在構造函數(shù)前加上 explicit 修飾詞, 則會禁止這種自動轉(zhuǎn)換,在這種情況下,即使將對應于構造函數(shù)參數(shù)類型的數(shù)據(jù)直接賦值給類變量,編譯器也會報錯。

下面以具體實例來說明。

建立people.cpp 文件,然后輸入下列內(nèi)容:

class People
{
public:
int age;
explicit People (int a)
 {
  age=a;
 }
};
void foo ( void )
{
 People p1(10);  //方式一
 People* p_p2=new People(10); //方式二
 People p3=10; //方式三
}
這段 C++ 程序定義了一個類 people ,包含一個構造函數(shù), 這個構造函數(shù)只包含一個整形參數(shù) a ,可用于在構造類時初始化 age 變量。

然后定義了一個函數(shù)foo,在這個函數(shù)中我們用三種方式分別創(chuàng)建了三個10歲的“人”。第一種是最一般的類變量聲明方式。第二種方式其實是聲明了一個people類的指針變量,然后在堆中動態(tài)創(chuàng)建了一個people實例,并把這個實例的地址賦值給了p_p2。第三種方式就是我們所說的特殊方式,為什么說特殊呢?我們都知道,C/C++是一種強類型語言,不同的數(shù)據(jù)類型是不能隨意轉(zhuǎn)換的,如果要進行類型轉(zhuǎn)換,必須進行顯式強制類型轉(zhuǎn)換,而這里,沒有進行任何顯式的轉(zhuǎn)換,直接將一個整型數(shù)據(jù)賦值給了類變量p3。

因此,可以說,這里進行了一次隱式類型轉(zhuǎn)換,編譯器自動將對應于構造函數(shù)參數(shù)類型的數(shù)據(jù)轉(zhuǎn)換為了該類的對象,因此方式三經(jīng)編譯器自動轉(zhuǎn)換后和方式一最終的實現(xiàn)方式是一樣的。

不相信? 耳聽為虛,眼見為實,讓我們看看底層的實現(xiàn)方式。

為了更容易比較方式一和方式三的實現(xiàn)方式,我們對上面的代碼作一點修改,去除方式二:

void foo ( void )
{
 People p1(10);  //方式一
 People p3=10; //方式三
}
去除方式二的原因是方式二是在堆上動態(tài)創(chuàng)建類實例,因此會有一些額外代碼影響分析。修改完成后,用下列命令編譯 people.cpp

$ gcc -S people.cpp

"-S"選項是GCC輸出匯編代碼。命令執(zhí)行后,默認生成people.s。 關鍵部分內(nèi)容如下:

.globl _Z3foov
.type _Z3foov, @function
_Z3foov:
.LFB5:
 pushl %ebp
.LCFI2:
 movl %esp, %ebp
.LCFI3:
 subl $24, %esp
.LCFI4:
 movl $10, 4(%esp)
 leal -4(%ebp), %eax
 movl %eax, (%esp)
 call _ZN6PeopleC1Ei
 movl $10, 4(%esp)
 leal -8(%ebp), %eax
 movl %eax, (%esp)
 call _ZN6PeopleC1Ei
 leave
 ret

看“.LCFI4” 行后面的東西,1-4行和5-8行幾乎一模一樣,1-4行即為方式一的匯編代碼,5-8即為方式三的匯編代碼。 細心的你可能發(fā)現(xiàn)2和6行有所不同,一個是 -4(%ebp) 而另一個一個是 -8(%ebp) ,這分別為類變量P1和P3的地址。

對于不可隨意進行類型轉(zhuǎn)換的強類型語言C/C++來說, 這可以說是C++的一個特性。哦,今天好像不是要說C++的特性,而是要知道explicit關鍵字的作用?

explicit關鍵字到底是什么作用呢? 它的作用就是禁止這個特性。如文章一開始而言,凡是用explicit關鍵字修飾的構造函數(shù),編譯時就不會進行自動轉(zhuǎn)換,而會報錯。

讓我們看看吧! 修改代碼:

class People
{
public:
int age;
explicit People (int a)
 {
  age=a;
 }
};
然后再編譯:
$ gcc -S people.cpp
編譯器立馬報錯:
people.cpp: In function ‘void foo()’:
people.cpp:23: 錯誤:請求從 ‘int’ 轉(zhuǎn)換到非標量類型 ‘People’

posted on 2009-07-24 16:49 yanghaibao 閱讀(244) 評論(0)  編輯 收藏 引用

導航

<2025年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

統(tǒng)計

常用鏈接

留言簿

隨筆分類

隨筆檔案

文章檔案

收藏夾

Good blogs

搜索

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            性欧美1819sex性高清| 亚洲影院在线观看| 久久久久久噜噜噜久久久精品| 国产日韩欧美另类| 久久亚洲欧美| 久久婷婷成人综合色| 亚洲国产精品第一区二区| 亚洲电影av在线| 欧美激情一区二区三区在线视频观看| 91久久久国产精品| 亚洲美女一区| 国产精品区一区| 久久久久五月天| 免费成人在线视频网站| 在线视频你懂得一区| 中文在线一区| 激情小说亚洲一区| 亚洲日本中文字幕区| 国产农村妇女精品| 欧美大片一区二区三区| 欧美日韩美女在线观看| 欧美一区二区在线播放| 久久久精品动漫| 亚洲精品久久久久久久久久久久 | 亚洲欧美日韩国产中文在线| 亚洲永久免费av| 影音先锋亚洲视频| 亚洲每日更新| 亚洲成人在线观看视频| 日韩午夜在线电影| 在线观看国产一区二区| 亚洲麻豆视频| 在线精品观看| 亚洲一区欧美一区| 亚洲精品日韩综合观看成人91| 亚洲婷婷国产精品电影人久久| 亚洲成人中文| 午夜精品视频一区| 在线亚洲精品福利网址导航| 久久久久九九九| 亚洲欧美日韩国产| 欧美激情第二页| 久久久久国产一区二区| 欧美日韩理论| 亚洲成人资源| 国产视频久久久久久久| 99热这里只有成人精品国产| 91久久在线| 久久久久国产精品一区| 欧美一级二区| 国产精品久久午夜夜伦鲁鲁| 亚洲欧洲精品一区二区精品久久久| 好吊色欧美一区二区三区四区| 亚洲一区二区伦理| 亚洲一区二区影院| 欧美国产欧美亚洲国产日韩mv天天看完整| 欧美在线播放一区二区| 国产精品久久久久久久久搜平片| 亚洲黄色免费电影| 亚洲激情电影中文字幕| 久久久精品性| 老司机一区二区三区| 国产亚洲毛片在线| 亚洲欧美日韩在线高清直播| 午夜精品一区二区三区在线视| 国产精品jizz在线观看美国| 日韩一级精品| 亚洲一区视频在线观看视频| 欧美日韩一二区| 一区二区三区av| 亚洲一区二区三区四区五区黄 | 欧美一区二区视频在线观看2020| 午夜精品999| 国产精品亚洲产品| 欧美一区二区三区啪啪| 久久综合九色综合久99| 雨宫琴音一区二区在线| 美女日韩欧美| 亚洲九九爱视频| 亚洲一区二区三区乱码aⅴ蜜桃女| 欧美日韩一级片在线观看| 在线视频精品一| 欧美在线视频不卡| 亚洲第一在线| 欧美巨乳在线观看| 亚洲小少妇裸体bbw| 久久精品在线免费观看| 亚洲激情欧美激情| 欧美日韩八区| 性欧美长视频| 亚洲第一在线综合网站| 亚洲伊人网站| 国内久久精品视频| 欧美精品一区二区三| 亚洲一区二区三区精品在线观看 | 亚洲风情亚aⅴ在线发布| 欧美激情精品久久久六区热门 | 久久久久久久一区二区| 亚洲国产99| 欧美性猛交99久久久久99按摩| 亚洲欧美日本伦理| 欧美激情按摩| 欧美一乱一性一交一视频| 尤物视频一区二区| 国产精品swag| 久久五月天婷婷| 国产精品99久久不卡二区| 久久亚洲精品中文字幕冲田杏梨| 99re6热在线精品视频播放速度| 国产欧美日韩另类视频免费观看| 免费精品视频| 午夜视频在线观看一区| 亚洲激情一区二区三区| 久久米奇亚洲| 亚洲与欧洲av电影| 亚洲日本成人| 国产一区二区丝袜高跟鞋图片| 欧美激情一区二区三区在线视频| 欧美在线你懂的| 亚洲天堂av在线免费观看| 欧美www在线| 久久精品视频一| 亚洲影院免费观看| 亚洲美女一区| 在线日韩av| 国产亚洲一区二区三区| 国产精品豆花视频| 欧美理论大片| 毛片一区二区三区| 久久久99国产精品免费| 午夜在线观看免费一区| 在线一区二区三区做爰视频网站 | 亚洲激精日韩激精欧美精品| 久久久综合免费视频| 欧美亚洲一区在线| 亚洲专区一二三| 亚洲一区综合| 亚洲一区二区免费看| 中日韩美女免费视频网站在线观看| 亚洲黄色小视频| 91久久国产精品91久久性色| 又紧又大又爽精品一区二区| 韩日精品视频| 激情久久五月天| 狠狠噜噜久久| 永久久久久久| 亚洲电影免费观看高清完整版在线 | 亚洲一级黄色片| 一二三区精品| 亚洲视频福利| 亚洲欧美日韩国产综合| 亚洲综合视频在线| 亚洲欧美在线网| 欧美一区久久| 久久久久久久久久久久久久一区 | 亚洲精品一区二区三区99| 91久久中文| 日韩午夜在线| 一本色道久久88综合日韩精品| 日韩一级在线观看| 一区二区三区鲁丝不卡| 亚洲影院色无极综合| 欧美一区视频| 欧美成人精品影院| 欧美日韩福利在线观看| 欧美午夜不卡| 国产视频一区在线| 精品动漫3d一区二区三区免费版| 亚洲电影观看| 亚洲视频网在线直播| 久久成人av少妇免费| 欧美va亚洲va日韩∨a综合色| 亚洲电影免费在线观看| 一区二区三区四区五区精品视频| 亚洲影视九九影院在线观看| 久久成人av少妇免费| 欧美激情一区二区三区在线视频| 国产精品theporn| 韩日视频一区| 在线一区二区三区四区| 久久久久中文| 欧美激情欧美激情在线五月| 在线综合亚洲| 久久天天狠狠| 国产精品海角社区在线观看| 亚洲电影免费观看高清完整版| 一本大道久久a久久精品综合| 欧美专区中文字幕| 亚洲人屁股眼子交8| 久久成人在线| 欧美丝袜一区二区| 亚洲国产精品第一区二区| 亚洲欧美999| 欧美黄免费看| 欧美一级黄色网| 欧美三级不卡| 亚洲精品乱码久久久久久黑人 | 亚洲老司机av| 久久久www免费人成黑人精品| 亚洲免费成人av|