私はクラスのようなboost::any
を実装しようとしています:どのように私は私の後押しを変更することができます::どのようなクラス
struct any
{
private:
struct holderBase
{
virtual ~holderBase(){}
};
template<typename T>
struct holder : public holderBase
{
T content;
holder(const T& value) : content(value){}
holder(const holder<T>& other) : content(other.content){}
};
holderBase *hl;
public:
template<typename T>
any(const T& data = T()) { hl = new holder<T>(data); }
any(const any& other) { hl = other.hl; }
template<typename T>
T get()
{
if(holder<T>* p_hl = dynamic_cast<holder<T>*>(hl))
return p_hl->content;
else
throw std::runtime_error("std::runtime_error");
}
};
私はデータを格納する(holderBase
によって継承されます)ホルダークラスを使用します。 テンプレートパラメータ(get()
関数)を必要としないように、any::get()
関数を変更する(またはコード全体を変更する)方法を教えてください。
@LucDanton:はい。私の悪い! – dirkgently