boost中的類型轉換操作符polymorphic_cast、polymorphic_downcast、lexical_cast、numeric_cas詳解

零、小序

標準C++中提供了四種類型轉換運算符dynamic_cast、static_cast、const_cast和reinterpret_cast,大大方便了程序員對類型轉換的使用,boost是C++程序員經常使用的第三方庫,它同樣提供了一些很好用的類型轉換操作,甚至有些使用起來比C++標準庫更方便。

有關C++標準庫提供的類型轉換運算符可以看這篇文章的詳細解讀:https://blog.csdn.net/toby54king/article/details/105310923

一、動態轉換操作符

1、polymorphic_cast和polymorphic_downcast

C++標準庫提供的動態轉換操作符dynamic_cast既可以向上轉換又可以向下轉換,而boost庫提供的類型轉換運算符把這兩種情況拆分開了。polymorphic_cast提供向上轉換的操作,polymorphic_downcast提供向下轉換的操作。

boost::polymorphic_cast內部使用了dynamic_cast,可以實現交叉轉換的類型轉換操作符,boost::polymorphic_cast能夠在錯誤的時候拋出 std::bad_cast 類型的異常。

boost::polymorphic_downcast類型轉換操作符只能用於向下轉換,它的內部使用 static_cast 實現類型轉換。 所以polymorphic_downcast並不動態檢查類型轉換是否合法,需要程序員自己確保轉換安全。

2、代碼示例

// BoostCastType.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//

#include <iostream>
#include <boost/cast.hpp>

using namespace std;
using namespace boost;

class Father
{
public:
	Father() { cout << "Father!" << endl; };
	~Father() {};
	virtual void MyPrintf()
	{
		cout << "this is a Father class!" << endl;
	}
private:

};

class Mother
{
public:
	Mother() { cout << "Mother!" << endl; };
	~Mother() {};
	virtual void MyPrintf()
	{
		cout << "this is a Mother class!" << endl;
	}
private:

};

class Child:public Father,public Mother
{
public:
	Child() {};
	~Child() {};
	virtual void MyPrintf()
	{
		cout << "this is a Child class!" << endl;
	}
private:

};

#define DELETE_PTR(p) {if(p!=nullptr){delete (p); (p)=nullptr;}}

int main()
{
	cout << "--------------boost庫中動態類型轉換---------------" << endl;
	Child *pChild = new Child;
	pChild->MyPrintf();

	Father *pFather = boost::polymorphic_cast<Father*>(pChild);
	pFather->MyPrintf();

	Mother *pMother = boost::polymorphic_cast<Mother*>(pFather);
	pMother->MyPrintf();
	
	Child *pTmpChild = boost::polymorphic_downcast<Child*>(pMother);
	pTmpChild->MyPrintf();
	
	// 只需要釋放這一個指針就行,其他指針的地址都指向這個指針的地址 
	DELETE_PTR(pChild);
    std::cout << "Hello World!\n";
	getchar();
}

運行結果:
在這裏插入圖片描述

二、流轉換操作符

1、lexical_cast

boost::lexical_cast 可以很容易的實現數字和字符串之間的轉換,它內部使用流(streams)執行轉換操作。 因此,只有那些重載了 operator<<() 和 operator>>() 這兩個操作符的類型可以轉換,如果轉換失敗,會拋出 boost::bad_lexical_cast 類型的異常,它繼承自 std::bad_cast。

2、代碼示例

// BoostLexicalCast.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//

#include <iostream>
#include <boost/lexical_cast.hpp>

using namespace std;
using namespace boost;

int main()
{
	cout << "--------------boost庫中類型轉換lexical_cast---------------" << endl;
	cout << "--------------lexical_cast將字符串轉換爲數值---------------" << endl;
	int iNum = boost::lexical_cast<int>("100");
	short sNum = boost::lexical_cast<short>("12");
	double dNum = boost::lexical_cast<double>("100.86");
	float fPai = boost::lexical_cast<float>("3.14159");
	cout << "iNum: " << iNum << endl;
	cout << "sNum: " << sNum << endl;
	cout << "dNum: " << dNum << endl;
	cout << "fPai: " << fPai << endl;

	cout << "--------------lexical_cast將數值字符串轉換爲字符串---------------" << endl;
	string iNumStr = boost::lexical_cast<string>(100);
	string sNumStr = boost::lexical_cast<string>(1200);
	string dNumStr = boost::lexical_cast<string>(100.86);
	string fPaiStr = boost::lexical_cast<string>(3.14159);
	string oxNumStr = boost::lexical_cast<string>(0x11);
	cout << "iNumStr: " << iNumStr << endl;
	cout << "sNumStr: " << sNumStr << endl;
	cout << "dNumStr: " << dNumStr << endl;
	cout << "fPaiStr: " << fPaiStr << endl;
	cout << "oxNumStr: " << oxNumStr << endl;

    std::cout << "Hello World!\n";
	getchar();
}

運行結果:
在這裏插入圖片描述

三、數值轉換操作符

1、numeric_cast

boost::numeric_cast可將一種數值類型轉換爲不同的數值類型,與C++類型轉換操作符相似,boost::numeric_cast會驗證在不改變數值的情況下轉換是否能夠發生,如果轉換不能發生,會拋出 boost::numeric::bad_numeric_cast 異常。

2、代碼示例

// BoostNumericCast.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//

#include <iostream>
#include <boost/numeric/conversion/cast.hpp>

using namespace std;
using namespace boost;

int main()
{
	cout << "--------------boost庫中類型轉換numeric_cast---------------" << endl;

	int iNum = 10086;
	float fNum = 3.14;
	int iTmpNum = boost::numeric_cast<int>(iNum);
	short sNum = boost::numeric_cast<short>(iNum);
	double dNum = boost::numeric_cast<double>(fNum);
	cout << "iNum: " << iNum << endl;
	cout << "fNum: " << fNum << endl;
	cout << "iTmpNum: " << iTmpNum << endl;
	cout << "sNum: " << sNum << endl;
	cout << "dNum: " << dNum << endl;

	cout << "--------------numeric_cast拋出異常---------------" << endl;
	try
	{
		int num = 0x10001;
		short sTmpNum = boost::numeric_cast<short>(num);
		cout << "sTmpNum="<< sTmpNum << std::endl;
	}
	catch (boost::numeric::bad_numeric_cast &e)
	{
		cerr << e.what() << std::endl;
	}

    std::cout << "Hello World!\n";
	getchar();
}

運行結果:
在這裏插入圖片描述

原創不易,點贊鼓勵一下吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章