複製代碼 代碼如下:
/*=============================================================================
# FileName: explicit_try.cc
# Desc: 驗證含有一個參數的非explicit建構函式是否可以拷貝初始化
=============================================================================*/
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
class People {
public:
People() = default;
People(string s):name(s) { }
string getName() const { return name; }
static vector<string> &getVector() { return name_arr; }
//隱式類型轉換,用string產生一個臨時量,因此可以綁定到const形參上
static void addToVector(const People &p) {
name_arr.push_back(p.getName());
}
private:
string name = "";
static vector<string> name_arr;
};
vector<string> People::name_arr = {};
int main(int argc, const char *argv[])
{
People p;
cout << "p :" << endl;
cout << p.getName() << endl;
People tom("tom");
People::addToVector(tom);
string Bob = "Bob";
People::addToVector(Bob);//隱式類型轉換
//People::addToVector("Bob");//只允許一步的隱式類型轉換
vector<string> v = People::getVector();
cout << "name_arr:" << endl;
for (const auto &p : v) {
cout << p << " ";
}
cout << endl;
string myName = "guo";
People guo = myName; //隱式類型轉換允許拷貝初始化形式的轉換
cout << guo.getName() << endl;
return 0;
}
下面再來一個例子
複製代碼 代碼如下:
#include <string>
#include <iostream>
using namespace std;
class Fruit //定義一個類,名字叫Fruit
{
string name; //定義一個name成員
string colour; //定義一個colour成員
public:
bool isSame(const Fruit &otherFruit) //期待的形參是另一個Fruit類對象,測試是否同名
{
return name == otherFruit.name;
}
void print() //定義一個輸出名字的成員print()
{
cout<<colour<<" "<<name<<endl;
}
Fruit(const string &nst,const string &cst = "green"):name(nst),colour(cst){} //建構函式
Fruit(){}
};
int main()
{
Fruit apple("apple");
Fruit orange("orange");
cout<<"apple = orange ?: "<<apple.isSame(orange)<<endl; //沒有問題,肯定不同
cout<<"apple = /"apple/" ?:"<<apple.isSame(string("apple")); //用一個string做形參?
return 0;
}
你會發現最後的使用上,我們用一個string類型作一個期待Fruit類形參的函數的參數,結果竟然得出了是true(1),不要感到奇怪,這就是我現在要講的東西,隱式類類型轉換:“可以用單個實參來調用的建構函式定義了從形參類型到該類型的一個隱式轉換。”(C++ Primer)首先要單個實參,你可以把建構函式colour的預設實參去掉,也就是定義一個對象必須要兩個參數的時候,檔案編譯不能通過。然後滿足這個條件後,系統就知道怎麼轉換了,不過這裡比較嚴格:)以前我們構造對象的時候Fruit apple("apple")其實也已經有了一個轉換,從const char *的C字串格式,轉為string,在這裡,你再apple.isSame("apple")的話,蠢系統不懂得幫你轉換兩次,所以你必須要用string()來先強制轉換,然後系統才知道幫你從string隱式轉換為Fruit,當然其實你自己也可以幫他完成。cout<<"apple = /"apple/" ?:"<<apple.isSame(Fruit("apple"));這樣。參考例子1.2 :Fruit apple = Fruit("apple"); //定義一個Fruit類對象apple。也就是這樣轉換的。不過這就叫顯式轉換了,我們不標出來,系統幫我們完成的,叫隱式的貝。這裡要說的是,假如你顯示轉換就可以不管有多少參數了,比如在前面提到的必須需要兩個參數的建構函式時的例子。
例:
複製代碼 代碼如下:
#include <string>
#include <iostream>
using namespace std;
class Fruit //定義一個類,名字叫Fruit
{
string name; //定義一個name成員
string colour; //定義一個colour成員
public:
bool isSame(const Fruit &otherFruit) //期待的形參是另一個Fruit類對象,測試是否同名
{
return name == otherFruit.name;
}
void print() //定義一個輸出名字的成員print()
{
cout<<colour<<" "<<name<<endl;
}
Fruit(const string &nst,const string &cst):name(nst),colour(cst){} //建構函式
Fruit(){}
};
int main()
{
Fruit apple("apple","green");
Fruit orange("orange","yellow");
cout<<"apple = orange ?: "<<apple.isSame(orange)<<endl; //沒有問題,肯定不同
cout<<"apple = /"apple/" ?:"<<apple.isSame(Fruit("apple","green")); //顯式轉換
return 0;
}
在你不想隱式轉換,以防使用者誤操作怎麼辦?C++提供了一種抑制建構函式隱式轉換的辦法,就是在建構函式前面加explicit關鍵字,你試試就知道,那時你再希望隱式轉換就會導致編譯失敗,但是,要說明的是,顯式轉換還是可以進行。