boost::static_pointer_cast、boost::dynamic_pointer_cast和boost::const_pointer_cast

当我们用“裸”指针进行类层次上的上下行转换时,可以使用dynamic_cast。当然我们也可以使用static_cast,只是dynamic_cast在进行下行转换的时候(即基类到派生类)具有类型检查功能,而static_cast没有。因此存在安全问题。

当我们使用智能指针时,如果需要进行类层次上的上下行转换时,可以使用boost::static_pointer_cast和boost::dynamic_pointer_cast。(C++11中也支持智能指针和转换,只是命名空间改成std即可)。

1. static_pointer_cast

#include 
#include 
#include 

using namespace std;

class CBase
{
public:
	CBase() { }
	virtual ~CBase() { }

	void myBase()
	{
		cout << "CBase::myBase" << endl;
	}
};

class CDerive : public CBase
{
public:
	CDerive() { }
	~CDerive() { }

	void myDerive()
	{
		cout << "CDerive::myDerive" << endl;
	}
};

int main(void)
{
	//上行的转换(派生类到基类的转换)
	boost::shared_ptr spDeriveUp;
	boost::shared_ptr spBaseUp;
	spDeriveUp = boost::make_shared();
	spBaseUp = boost::static_pointer_cast(spDeriveUp);
	spBaseUp->myBase();

	//下行的转换(基类到派生类的转换)这是不安全的
	boost::shared_ptr spDeriveDown;
	boost::shared_ptr spBaseDown;
	spBaseDown = boost::make_shared();
	spDeriveDown = boost::static_pointer_cast(spBaseDown);
	spDeriveDown->myBase();		//不允许访问myDerive

	return 0;
}

2. dynamic_pointer_cast

#include 
#include 
#include 

using namespace std;

class CBase
{
public:
	CBase() { }
	virtual ~CBase() { }

	void myBase()
	{
		cout << "CBase::myBase" << endl;
	}
};

class CDerive : public CBase
{
public:
	CDerive() { }
	~CDerive() { }

	void myDerive()
	{
		cout << "CDerive::myDerive" << endl;
	}
};

int main(void)
{
	//上行的转换(派生类到基类的转换)
	boost::shared_ptr spDeriveUp;
	boost::shared_ptr spBaseUp;
	spDeriveUp = boost::make_shared();
	spBaseUp = boost::dynamic_pointer_cast(spDeriveUp);
	spBaseUp->myBase();

	//下行的转换(基类到派生类的转换)
	boost::shared_ptr spDeriveDown;
	boost::shared_ptr spBaseDown;
	spBaseDown = boost::make_shared();
	spDeriveDown = boost::dynamic_pointer_cast(spBaseDown);
	if (spDeriveDown == NULL)	//由于会进行类型的检查,这边是返回NULL
		cout << "spDeriveDown is null" << endl;

	return 0;
}

3. const_pointer_cast

#include 
#include 
#include 

using namespace std;

int main(void)
{
	{
		boost::shared_ptr spInt;
		boost::shared_ptr spConstInt;

		spInt = boost::make_shared(100);
		spConstInt = boost::const_pointer_cast(spInt);
		cout << "*spConstInt = " << *spConstInt << endl;
	}

	{
		boost::shared_ptr spInt;
		boost::shared_ptr spConstInt;
		//事实上不允许通过spInt修改值
		spConstInt = boost::make_shared(100);
		spInt = boost::const_pointer_cast(spConstInt);
		cout << "*spInt = " << *spInt << endl;
	}


	return 0;
}


你可能感兴趣的:(c++,boost)