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

12 篇文章 0 订阅

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

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

1. static_pointer_cast

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

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<CDerive> spDeriveUp;
	boost::shared_ptr<CBase> spBaseUp;
	spDeriveUp = boost::make_shared<CDerive>();
	spBaseUp = boost::static_pointer_cast<CBase>(spDeriveUp);
	spBaseUp->myBase();

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

	return 0;
}

2. dynamic_pointer_cast

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

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<CDerive> spDeriveUp;
	boost::shared_ptr<CBase> spBaseUp;
	spDeriveUp = boost::make_shared<CDerive>();
	spBaseUp = boost::dynamic_pointer_cast<CBase>(spDeriveUp);
	spBaseUp->myBase();

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

	return 0;
}

3. const_pointer_cast

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

using namespace std;

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

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

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


	return 0;
}


  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
boost::asio::high_resolution_timer是一个定时器类,用于在boost::asio中进行高分辨率的定时操作。 boost::asio是一个用于网络和异步编程的C++。它提供了丰富的功能,包括异步IO操作、定时器、socket通信等。其中,boost::asio::high_resolution_timer是其中的一个定时器类,它使用了高分辨率的时钟来进行精确的定时操作。 使用boost::asio::high_resolution_timer,我们可以创建一个定时器对象,并设置定时的时间间隔。可以使用成员函数expires_from_now()指定定时的时间间隔,参数为一个duration类型的对象,表示时间间隔的长度。 例如,以下代码创建了一个定时器对象timer,设置了定时时间间隔为1秒: boost::asio::high_resolution_timer timer(io_context); timer.expires_from_now(boost::posix_time::seconds(1)); 然后,我们可以调用定时器对象的async_wait()函数来启动定时器,并指定一个回调函数,在定时器超时时被调用。回调函数可以是一个lambda函数,也可以是一个函数对象。 例如,以下代码定义了一个lambda函数作为回调函数: timer.async_wait([](const boost::system::error_code& ec) { if (!ec) { // 定时器超时,执行相应操作 } }); 在定时器超时时,回调函数会被触发,并执行相应操作。 总之,boost::asio::high_resolution_timer是一个用于高分辨率定时操作的定时器类,可以帮助我们在异步编程中进行精确的定时操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值