C++ Any类型实现
来源:互联网 发布:网络爬虫软件下载 编辑:程序博客网 时间:2024/06/05 21:56
C++ Any类型实现
在java或c#中,Object类型对象可以指向任意类型的变量,因为所有的类默认都从Object类继承。但是在c++中,没有类似Object类这样的类型,而很多时候,为了设计出通用的程序,往往需要类似于Object类型作为参数或者返回值。
class any{public: // structors any() : content(0) { } template<typename ValueType> any(const ValueType & value) : content(new holder<ValueType>(value)) { } any(const any & other) : content(other.content ? other.content->clone() : 0) { } ~any() { delete content; }public: // modifiers any & swap(any & rhs) { std::swap(content, rhs.content); return *this; } template<typename ValueType> any & operator=(const ValueType & rhs) { any(rhs).swap(*this); return *this; } any & operator=(any rhs) { rhs.swap(*this); return *this; }public: // queries bool empty() const { return !content; } const std::type_info & type() const { return content ? content->type() : typeid(void); }public: // types (public so any_cast can be non-friend) class placeholder { public: // structors virtual ~placeholder() { } public: // queries virtual const std::type_info & type() const = 0; virtual placeholder * clone() const = 0; }; template<typename ValueType> class holder : public placeholder { public: // structors holder(const ValueType & value) : held(value) { } public: // queries virtual const std::type_info & type() const { return typeid(ValueType); } virtual placeholder * clone() const { return new holder(held); } public: // representation ValueType held; private: // intentionally left unimplemented holder & operator=(const holder &); };public: // representation (public so any_cast can be non-friend) placeholder * content;};template<typename ValueType> ValueType any_cast(const any& operand) { assert(operand.type() == typeid(ValueType)); return static_cast<any::holder<ValueType> *>(operand.content)->held;}
参考链接:http://www.cnblogs.com/feixue/p/boost-any.html
阅读全文
0 0
- C++ Any类型实现
- boost::any类型实现原理
- boost.any实现任意类型存储
- c++任意类型Any类的实现
- c++任意类型Any类的实现
- 万能类型boost::any
- 模仿boost::any类型
- 任意类型类Any
- 万能类型boost::any
- C++ 任意类型Any
- Koltin Any 类型
- OGRE中的任意类型Any
- OGRE中的任意类型 Any
- Swift Any和AnyObject类型
- any-----安全存储任意类型
- boost any 实现技法
- boost::any实现
- boost::any实现分析
- 异常处理
- 学习VALN相关笔记
- Hibernate框架
- Java 并发编程内部分享PPT分享
- Android查看磁盘分区
- C++ Any类型实现
- FZU2254 英语考试(最小生成树,Prim)(福州大学第十四届程序设计竞赛)
- 关于sampling softmax 中重要性采样的论文阅读笔记
- HDU 1325 Is It A Tree?
- OpenCV 2.4.9+vs2013的配置
- C++排序算法
- jzoj 模拟赛总结(2017.07.13)
- Android Studio技巧
- 基础