找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4784|回复: 1

关于boost::any的几个问题

[复制链接]
发表于 2012-3-22 21:18:53 | 显示全部楼层 |阅读模式

class CAny
{
        class HolderBase
        {
        public:
                virtual ~HolderBase(){}
        };

        template<typename T>
        class Holder : public HolderBase
        {
                friend CAny;

                T t_;
        public:
                Holder(const T& t)
                        :t_(t)
                {
                }
        };

        HolderBase *hb_;
//        map<string, CAny> member_;
public:
        CAny()
        {
                hb_ = 0;
        }

        ~CAny()
        {
                delete hb_;
        }

        CAny(const CAny& xld);

        template<typename T>
        CAny(const T& d);

        CAny& operator=(const CAny& xld);

        template<typename T>
        CAny& operator=(const T& d);

        CAny operator[](const string& key);
};

CAny::CAny(const CAny& xld)
{
        hb_ =  new Holder<CAny>(xld);
}

template<typename T>
CAny::CAny(const T& d)
{
        hb_ = new Holder<T>(d);
}

CAny& CAny::operator=(const CAny& xld)
{
        delete hb_;
        hb_ = new Holder<CAny>(xld);

        return *this;
}

template<typename T>
CAny& CAny::operator=(const T& d)
{
        delete hb_;
        hb_ = new Holder<T>(d);

        return *this;

}

CAny CAny::operator[](const string& key)
{
        //return (*(*dynamic_cast<Holder<map<string, shared_ptr<CAny> > > *>(hb_)))[key];
        typedef map<string, CAny> MapHolder;

        Holder<MapHolder> *ptr = (Holder<MapHolder> *)hb_;

        return (*ptr)[key];
}


我实现了一个类似于any的东西,但是编译不过
我想让他能够有如下功能
CAny x;
x["name"] = string("brightsun")
CAny y;
y["country"] = string("china")
y["province"] = string("beijing")

x["address"] = y;
x["age"] = 100;


cout << x << endl;

请达人解答,谢谢!
发表于 2012-5-4 10:49:24 | 显示全部楼层
你这个和boost::any语义不一样吧,倒是有点像std::map<boost::any>
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-29 00:49 , Processed in 0.010578 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表