boost::static_pointer_cast、boost::dynamic_pointer_cast和boost::const_pointer_cast
来源:互联网 发布:android 屏保源码 编辑:程序博客网 时间:2024/06/05 15:24
当我们用“裸”指针进行类层次上的上下行转换时,可以使用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();//不允许访问myDerivereturn 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)//由于会进行类型的检查,这边是返回NULLcout << "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;}
阅读全文
0 0
- boost::static_pointer_cast、boost::dynamic_pointer_cast和boost::const_pointer_cast
- std::static_pointer_cast, std::dynamic_pointer_cast, std::const_pointer_cast
- boost shared_ptr 基类转到子类 boost::dynamic_pointer_cast
- Boost
- boost
- boost
- Boost
- boost
- boost
- Boost
- Boost
- Boost
- boost
- boost
- Boost
- boost
- boost
- boost
- Linux url调测工具--curl
- 第五章 常用Lua开发库3-模板渲染
- 剑指offer-二叉树中和为某一值的路径
- Linux下常用的C/C++开源Socket库
- ajax跨域请求springmvc传递json对象参数
- boost::static_pointer_cast、boost::dynamic_pointer_cast和boost::const_pointer_cast
- 同步与异步,阻塞与非阻塞
- Leetcode 309. Best Time to Buy and Sell Stock with Cooldown
- 基于TCP的多进程和多线程服务器
- ajax如何处理服务器返回的3种数据格式
- 机器学习—推荐系统的设计
- centos防火墙关闭操作
- 缓存篇第一节:ehcache
- ArcGIS水文分析实战教程(8)水库库容计算