目錄
- 1. 轉換建構式
- 2. explicit關鍵字
- 3. 型別轉換函式
1. 轉換建構式
類的建構式可以定義不同型別的引數,當引數滿足下列條件時,就可稱其為轉換建構式,
- 函式僅有一個引數
- 引數是基本型別或者其他型別別
其中,有一種特殊情形,也可構成轉換建構式,
- 函式有多個引數,但除了第一個引數外,其余都是默認引數
- 第一個引數是基本型別或者其他型別別
- 函式呼叫時只使用一個引數
C++編譯器在進行編譯作業時,會盡力嘗試讓原始碼通過編譯,因此如果碰到了這樣的代碼Test t = 100,編譯器不會立即報錯,而是進行以下嘗試:
- 查找類中是否有定義轉換建構式
- 如果定義了Test(int i),則先呼叫Test(100)將int型別隱式轉換為Test型別,再賦值給t,編譯通過
- 如果沒有定義,編譯才報錯
#include <iostream>
using namespace std;
class Test
{
int mValue;
public:
Test()
{
mValue = https://www.cnblogs.com/songhe364826110/p/0;
}
//轉換建構式
Test(int i)
{
mValue = i;
}
//當僅以第一個引數呼叫時,該函式等價于Test(int i),也是轉換建構式
/*Test(int i, int j = 0, int k = 0)
{
mValue = i;
}*/
Test operator + (const Test &p)
{
Test ret(mValue + p.mValue);
return ret;
}
int value()
{
return mValue;
}
};
int main()
{
Test t = 5; // Test t = Test(5);
Test r = t + 10; // Test r = t + Test(10);
cout <<"t.value = "https://www.cnblogs.com/songhe364826110/p/<< t.value() << endl;
cout <<"r.value = "https://www.cnblogs.com/songhe364826110/p/<< r.value() << endl;
return 0;
}

可以看到,當定義了轉換建構式時,編譯器盡力嘗試的結果是隱式型別轉換,而隱式型別轉換
- 有可能會讓程式以意想不到的方式作業
- 是工程中BUG的重要來源,應該盡力避免
2. explicit關鍵字
- 在工程中可以使用explicit關鍵字修飾轉換建構式,從而杜絕編譯器的轉換嘗試
- 轉換建構式被explicit修飾時只能使用顯式的強制型別轉換
- 作為編程的一般性原則,建議給所有的建構式都加上explicit關鍵字
#include <iostream>
using namespace std;
class Test
{
int mValue;
public:
explicit Test()
{
mValue = https://www.cnblogs.com/songhe364826110/p/0;
}
explicit Test(int i)
{
mValue = i;
}
//當僅以第一個引數呼叫時, 該函式等價于Test(int i), 也是轉換建構式, explicit有效且有必要
/*explicit Test(int i, int j = 0, int k = 0)
{
mValue = i;
}*/
Test operator + (const Test &p)
{
Test ret(mValue + p.mValue);
return ret;
}
int value()
{
return mValue;
}
};
int main()
{
//Test t = 5; // Error
//Test r = t + 10; // Error
Test t = static_cast(5);
Test r = t + static_cast(10);
cout <<"t.value = "https://www.cnblogs.com/songhe364826110/p/<< t.value() << endl;
cout <<"r.value = "https://www.cnblogs.com/songhe364826110/p/<< r.value() << endl;
return 0;
}

當使用了explicit關鍵字后,如果main()使用40-41行替換43-44行,編譯會直接報錯

3. 型別轉換函式
轉換建構式可以將其他型別轉換為型別別,而型別轉換函式則可以將型別別轉換到其他型別,包括普通型別和其他型別別,
- 型別轉換函式是轉換建構式的逆程序,它們具有同等的地位
- 編譯器也能夠使用型別轉換函式進行隱式轉換,從而盡力讓原始碼通過編譯
- 當目標型別是其他型別別時,型別轉換函式可能與轉換建構式沖突
定義型別轉換函式需要用到operator關鍵字,其語法規則為
operator TargetType ()
{
TargetType ret;
//......
return ret;
}
當編譯器遇到Test t(1); int i = t;這樣的代碼時,不會立即報錯,而是進行以下嘗試
- 查看Test類中是否有定義型別轉換函式
operator int () - 如果有定義,則進行隱式轉換,先呼叫型別轉換函式將t轉換為int,再賦值給i,編譯通過
- 如果沒有定義,編譯才報錯
#include <iostream>
using namespace std;
class Test;
class Value
{
int mValue;
public:
Value(int i = 0)
{
mValue = https://www.cnblogs.com/songhe364826110/p/i;
}
//如果不加explicit,會與Test中的operator Value ()沖突,產生二義性
explicit Value(Test &t)
{
}
int value()
{
return mValue;
}
};
class Test
{
private:
int mValue;
public:
Test(int i = 0)
{
mValue = i;
}
int value()
{
return mValue;
}
operator int ()
{
return mValue;
}
operator Value ()
{
Value ret(mValue);
return ret;
}
};
int main()
{
Test t(100);
int i = t;
Value v = t;
cout <<"i = " << i << endl;
cout << "v.value = "https://www.cnblogs.com/songhe364826110/p/<< v.value() << endl;
return 0;
}

和轉換建構式不同,型別轉換函式沒有類似explicit這種杜絕機制,也就是說,只要定義了型別轉換函式,我們就無法抑制編譯器的隱式呼叫,
因此,在工程中,通常不會使用型別轉換函式,而是以toType()的public成員函式來代替型別轉換函式,
#include <iostream>
using namespace std;
class Test;
class Value
{
int mValue;
public:
Value(int i = 0)
{
mValue = https://www.cnblogs.com/songhe364826110/p/i;
}
//如果不加explicit,會與Test中的operator Value ()沖突,產生二義性
explicit Value(Test &t)
{
}
int value()
{
return mValue;
}
};
class Test
{
private:
int mValue;
public:
Test(int i = 0)
{
mValue = i;
}
int value()
{
return mValue;
}
/*
* 工程中不用且不推薦的方式
*/
/*operator int ()
{
return mValue;
}
operator Value ()
{
Value ret(mValue);
return ret;
}*/
/*
* 工程中常用且推薦的方式:提供toType()的public成員函式
*/
int toInt()
{
return mValue;
}
Value toValue()
{
Value ret(mValue);
return ret;
}
};
int main()
{
Test t(100);
int i = t.toInt();
Value v = t.toValue();
cout <<"i = " << i << endl;
cout << "v.value = "https://www.cnblogs.com/songhe364826110/p/<< v.value() << endl;
return 0;
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/103679.html
標籤:C++
上一篇:演算法分析之猴子吃桃
下一篇:建構式與解構式呼叫技巧
