在C++程序中很少有人去使用explicit關鍵字,不可否認,在平時的實踐中確實很少能用的上。再說C++的功能強大,往往一個問題可以利用好幾種C++特性去解決。但稍微留心一下就會發現現有的MFC庫或者C++標準庫中的相關類聲明中explicit出現的頻率是很高的。瞭解explicit關鍵字的功能及其使用對於我們閱讀使用庫是很有幫助的,而且在編寫自己的代碼時也可以嘗試使用。既然C++語言提供這種特性,我想在有些時候這種特性將會非常有用。
按默認規定,只用傳一個參數的構造函數也定義了一個隱式轉換。舉個例子:
(下面這個CExample沒有什麼實際的意義,主要是用來說明問題)
-
-
#pragma once
-
class CExample
-
{
-
public:
-
CExample(void);
-
public:
-
~CExample(void);
-
public:
-
int m_iFirst;
-
int m_iSecond;
-
public:
-
CExample(int iFirst, int iSecond = 4);
-
};
-
-
#include "StdAfx.h"
-
#include "Example.h"
-
CExample::CExample(void)
-
: m_iFirst(0)
-
{
-
}
-
CExample::~CExample(void)
-
{
-
}
-
CExample::CExample(int iFirst, int iSecond):m_iFirst(iFirst), m_iSecond(iSecond)
-
{
-
}
-
-
...
-
#include "Example.h"
-
int _tmain(int argc, _TCHAR* argv[])
-
{
-
CExample objOne;
-
CExample objTwo(12, 12);
-
CExample objThree(12);
-
CExample objFour = 12;
-
-
return 0;
-
}
如果在構造函數聲明中加入關鍵字explicit,如下
explicit CExample(int iFirst, int iSecond = 4);
那麼CExample objFour = 12; 這條語句將不能通過編譯。在vs05下的編譯錯誤提示如下
error C2440: 'initializing' : cannot convert from 'int' to 'CExample'
Constructor for class 'CExample' is declared 'explicit'
對於某些類型,這一情況非常理想。但在大部分情況中,隱式轉換卻容易導致錯誤(不是語法錯誤,編譯器不會報錯)。隱式轉換總是在我們沒有察覺的情況下悄悄發生,除非有心所爲,隱式轉換常常是我們所不希望發生的。通過將構造函數聲明爲explicit(顯式)的方式可以抑制隱式轉換。也就是說,explicit構造函數必須顯式調用。
引用一下Bjarne Stroustrup的例子:
-
class String{
-
explicit String(int n);
-
String(const char *p);
-
};
-
String s1 = 'a';
-
String s2(10);
-
String s3 = String(10);
-
String s4 = "Brian";
-
String s5("Fawlty");
-
void f(String);
-
String g()
-
{
-
f(10);
-
f("Arthur");
-
return 10;
-
}
在實際代碼中的東西可不像這種故意造出的例子。
發生隱式轉換,除非有心利用,隱式轉換常常帶來程序邏輯的錯誤,而且這種錯誤一旦發生是很難察覺的。
原則上應該在所有的構造函數前加explicit關鍵字,當你有心利用隱式轉換的時候再去解除explicit,這樣可以大大減少錯誤的發生。