4.6 使用第三方库
以上介绍了Visual C++对对象赋值、转换及字符编码转换的方法,实际上还有一些好用的第三方类库用以辅助C++程序员完成对象处理,比较著名的就是boost。本节简单介绍boost库中与数值相关的boost::any、boost::lexical_cast,以及有理数类boost::rational。
4.6.1 万能类型boost::any
boost库提供了any类,boost::any是一个能保存任意类型值的类,这一点有点像variant类型,不过variant由于采用了一个巨大的union,效率非常低。而boost利用模板,保存的时候并不改变值的类型,只是在需要的时候才提供方法让用户进行类型判断及取值。
boost::any几乎可以用来存储任何数据类型:
- boost::anyai,as;
- ai=100;
- as=string("hello");
需要的时候,我们又可以使用any_cast将原来的数据还原:
- inti=boost::any_cast<int>(ai);
- strings=boost::any_cast<string>(as);
当这种转换发生类型不匹配时,会有异常bad_any_cast发生:
- try
- {
- inti=boost::any_cast<int>(as);
- }
- catch(boost::bad_any_cast&e)
- {
- }
在传统的C++程序中,为了支持各种数据类型,我们不得不使用万能指针"void *",但是很遗憾的是,基于万能指针的转换是不安全的,"void*"缺少类型检查。所以,我们建议大家尽量使用any类。
现在动手
编写如下程序,体验如何使用boost::any来完成对象类型转换。
【程序 4-10】使用boost::any完成对象类型转换
- 01#include"stdafx.h"
- 02#include"boost/any.hpp"
- 03#include<string>
- 04
- 05usingnamespacestd;
- 06usingnamespaceboost;
- 07
- 08classCat
- 09{
- 10};
- 11
- 12voidprint(anyit)
- 13{
- 14if(it.empty())
- 15{
- 16printf("nothing!\r\n");
- 17return;
- 18}
- 19
- 20if(it.type()==typeid(int))
- 21{
- 22printf("integer:%d\r\n",any_cast<int>(it));
- 23return;
- 24}
- 25
- 26if(it.type()==typeid(string))
- 27{
- 28printf("string:%s\r\n",any_cast<string>(it).c_str());
- 29return;
- 30}
- 31
- 32if(it.type()==typeid(CString))
- 33{
- 34_tprintf(_T("CString:%s\r\n"),any_cast<CString>(it));
- 35return;
- 36}
- 37
- 38if(it.type()==typeid(Cat))
- 39{
- 40_tprintf(_T("oops!acat!\r\n"));
- 41return;
- 42}
- 43}
- 44
- 45intmain()
- 46{
- 47print(100);
- 48
- 49anyas[]={any(),100,string("hello"),CString("world"),Cat()};
- 50for(inti=0;i<sizeof(as)/sizeof(as[0]);i++)
- 51{
- 52print(as[i]);
- 53}
- 54
- 55return0;56}
结果输出如图4-18所示。
![]() |
(点击查看大图)图4-18 运行结果 |
光盘导读
该项目对应于光盘中的目录"\ch04\BoostAnyTest"。
===========================================
以上摘自《把脉VC++》第4.6.1小节的内容,如果你想与我交流,请点击如下链接加我为好友:http://student.youkuaiyun.com/invite.php?u=113292&c=8913f87cffe7d533