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();
}

运行结果:
在这里插入图片描述

原创不易,点赞鼓励一下吧!

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