c++中的explicit關鍵字用來修飾類的建構函式,表明該建構函式是顯式的,既然有"顯式"那麼必然就有"隱式",那麼什麼是顯示而什麼又是隱式的呢?
如果c++類的建構函式有一個參數,那麼在編譯的時候就會有一個預設的轉換操作:將該建構函式對應資料類型的資料轉換為該類對象,如下面所示:
class MyClass
{
public:
MyClass( int num );
}
....
MyClass obj = 10; //ok,convert int to MyClass
在上面的代碼中編譯器自動將整型轉換為MyClass類對象,實際上等同於下面的操作:
MyClass temp(10);
MyClass obj = temp;
上面的所有的操作即是所謂的"隱式轉換"。
如果要避免這種自動轉換的功能,我們該怎麼做呢?嘿嘿這就是關鍵字explicit的作用了,將類的建構函式聲明為"顯示",也就是在聲明建構函式的時候前面添加上explicit即可,這樣就可以防止這種自動的轉換操作,如果我們修改上面的MyClass類的建構函式為顯示的,那麼下面的代碼就不能夠編譯通過了,如下所示:
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;
}
}
用於使用者自訂類型的建構函式,指定它是預設的建構函式,不可用於轉換建構函式.因為建構函式有三種:1拷貝建構函式2轉換建構函式3一般的建構函式(我自己的術語^_^)
另:如果一個類或結構存在多個建構函式時,explicit 修飾的那個建構函式就是預設的
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.
網上找的講的最好的貼:
C++ 中 explicit 關鍵字的作用
在 C++ 中, 如果一個類有只有一個參數的建構函式,C++ 允許一種特殊的聲明類變數的方式。在這種情況下,可以直接將一個對應於建構函式參數類型的資料直接賦值給類變數,編譯器在編譯時間會自動進行類型轉換,將對應於建構函式參數類型的資料轉換為類的對象。 如果在建構函式前加上 explicit 修飾詞, 則會禁止這種自動轉換,在這種情況下,即使將對應於建構函式參數類型的資料直接賦值給類變數,編譯器也會報錯。
下面以具體執行個體來說明。
建立people.cpp 檔案,然後輸入下列內容:
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 ,包含一個建構函式, 這個建構函式只包含一個整形參數 a ,可用於在構造類時初始化 age 變數。
然後定義了一個函數foo,在這個函數中我們用三種方式分別建立了三個10歲的“人”。第一種是最一般的類變數聲明方式。第二種方式其實是聲明了一個people類的指標變數,然後在堆中動態建立了一個people執行個體,並把這個執行個體的地址賦值給了p_p2。第三種方式就是我們所說的特殊方式,為什麼說特殊呢?我們都知道,C/C++是一種強型別語言,不同的資料類型是不能隨意轉換的,如果要進行類型轉換,必須進行顯式強制類型轉換,而這裡,沒有進行任何顯式的轉換,直接將一個整型資料賦值給了類變數p3。
因此,可以說,這裡進行了一次隱式類型轉換,編譯器自動將對應於建構函式參數類型的資料轉換為了該類的對象,因此方式三經編譯器自動轉換後和方式一最終的實現方式是一樣的。
不相信? 耳聽為虛,眼見為實,讓我們看看底層的實現方式。
為了更容易比較方式一和方式三的實現方式,我們對上面的代碼作一點修改,去除方式二:
void foo ( void )
{
People p1(10); //方式一
People p3=10; //方式三
}
去除方式二的原因是方式二是在堆上動態建立類執行個體,因此會有一些額外代碼影響分析。修改完成後,用下列命令編譯 people.cpp
$ gcc -S people.cpp
"-S"選項是GCC輸出彙編代碼。命令執行後,預設產生people.s。 關鍵區段內容如下:
.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即為方式三的彙編代碼。 細心的你可能發現2和6行有所不同,一個是 -4(%ebp) 而另一個一個是 -8(%ebp) ,這分別為類變數P1和P3的地址。
對於不可隨意進行類型轉換的強型別語言C/C++來說, 這可以說是C++的一個特性。哦,今天好像不是要說C++的特性,而是要知道explicit關鍵字的作用?
explicit關鍵字到底是什麼作用呢? 它的作用就是禁止這個特性。如文章一開始而言,凡是用explicit關鍵字修飾的建構函式,編譯時間就不會進行自動轉換,而會報錯。
讓我們看看吧! 修改代碼:
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’ 轉換到非標量類型 ‘People’