亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問板塊 發(fā)新帖
查看: 2264 | 回復(fù): 1
打印 上一主題 下一主題

[C++] 我自己簡(jiǎn)化的boost::any怎么能存取簡(jiǎn)單類型無(wú)法存取類? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2011-10-28 12:18 |只看該作者 |倒序?yàn)g覽
自己簡(jiǎn)化了一下Any:
class Any {
        public:
                Any() : content(0){}
                template<typename ValType>
                Any(const ValType & value) : content(new holder<ValType>(value)){}

                Any(const Any & other) : content(other.content ? other.content->clone() : 0){}

                ~Any(){delete content;}

        public:
                template<typename ValType>
                Any & operator=(const ValType & rhs){
                        Any(rhs).swap(*this);        return *this;
                }
                bool empty() const{return !content;}
                const std::type_info & type() const        {
                        return content ? content->type() : typeid(void);
                }
        private:
                Any & swap(Any & rhs){
                        std::swap(content, rhs.content);
                        return *this;
                }

        private:
                class placeholder{
                public:
                        virtual ~placeholder(){}
                        virtual const std::type_info & type() const = 0;
                        virtual placeholder * clone() const = 0;
                };

                template<typename ValType>
                class holder : public placeholder{
                public:
                        holder(const ValType & value) : held(value){}
                public:
                        virtual const std::type_info & type() const        {return typeid(ValType);}

                        virtual placeholder * clone() const        {return new holder(held);}
                public:
                        ValType held;
                };

        private:
                template<typename ValType>
                friend ValType * any_cast(Any *);

                placeholder * content;

        };

        template<typename ValType>
        inline ValType * any_cast(Any * any){
                return any && any->type() == typeid(ValType)?
                        &static_cast<Any::holder<ValType> *>(any->content)->held : 0;
        }

        template<typename ValType>
        ValType any_cast(const Any &any){
                ValType * val = any_cast<ValType>(&any);
                if(val == 0)        throw "invalid type-casting";
                return *val;
        }

之后想用于map<string,Any>,往里存取double等簡(jiǎn)單類型無(wú)誤,例如:
mp["ddd"]= val;
但放入類就有問題,例如:
class A
{
        public:
                enum ATYPE{AT_X,AT_Y};
                virtual ATYPE getType() = 0;
};
class X:public A
{
        public:
                ATYPE getType(){return AT_X;};
                void print();
        public:
                string x1;
                double x2;
};

一樣:mp["XXX"] = val;注意這里可以用type()取類型。
取的時(shí)候:

        std::map<std::string,Any>::iterator it(mp.find(key));
        if(it == mp.end())        return Any(std::string(""));
        return it->second;
到it->second返回時(shí)就segment fault。在這里想用type()取類型也一樣segment fault,不知錯(cuò)在哪兒。

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2011-10-28 17:34 |只看該作者
還是靠自己啊。做了兩件事:
1、刪除.o重新編譯。
2、ValType any_cast(const Any &any){
                 ValType * val = any_cast<ValType>(&any);
                 if(val == 0)        throw "invalid type-casting";
                 return *val;
         }
把參數(shù)改為Any any,我的編譯器不支持這玩意,只能犧牲效率了。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP