教育目的でシンプルboost::any
なクラスを作成していますが、保存された値にアクセスする方法がわかりません。値を完全に設定できますが、「ホルダー」クラスのメンバーにアクセスしようとすると、コンパイラーは、派生元のクラスにメンバーが見つからないと文句を言うだけです。virtual
テンプレートのため、メンバーを宣言できません。
関連するコードは次のとおりです。
class Element
{
struct ValueStorageBase
{
};
template <typename Datatype>
struct ValueStorage: public ValueStorageBase
{
Datatype Value;
ValueStorage(Datatype InitialValue)
{
Value = InitialValue;
}
};
ValueStorageBase* StoredValue;
public:
template <typename Datatype>
Element(Datatype InitialValue)
{
StoredValue = new ValueStorage<Datatype>(InitialValue);
}
template <typename Datatype>
Datatype Get()
{
return StoredValue->Value; // Error: "struct Element::ValueStorageBase" has no member named "Value."
}
};