我正在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."
}
};