访问类中的值类似于 boost::any
我正在制作一个简单的类似 boost::any 的类用于教育目的,但我不知道如何访问存储的值。我可以完美地设置该值,但是当我尝试访问“holder”类中的任何成员时,编译器只是抱怨在它派生的类中找不到该成员。由于模板的原因,我无法将成员声明为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."
}
};
I'm making a simple boost::any
-like class for educational purposes, but I can't figure out how to access the stored value. I can set the value perfectly, but when I try to access any member in the "holder" class the compiler just complains that the member wasn't found in the class it was derived from. I can't declare the members as virtual
because of the templates.
Here's the relevant code:
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."
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将虚拟函数添加到模板中很好 - 只是函数本身不能是模板。模板化的类或结构仍然可以有虚函数。你需要使用dynamic_cast的魔力。
如果您将 Get 更改为返回
Datatype*
,则可以返回NULL
而不是抛出异常。您还没有处理StoredValue
先前值的内存,但我将其留给您。It's fine to add virtual functions to templates- just the functions themselves cannot be templates. A templated class or struct can still have virtual functions just fine. You need to use the magic of dynamic_cast.
If you change Get to return a
Datatype*
you can returnNULL
instead of throwing. You also haven't handled the memory of the previous value ofStoredValue
, but I'm leaving that up to you.您需要首先将其转换为
ValueStorage
。还将虚拟析构函数添加到 ValueStorageBase 类,以具有多态类。没有它,您将无法运行时检查您的转换是否正常:)。
之后你可以写:
You need to cast it to
ValueStorage
first.Also add virtual destructur to ValueStorageBase class, to have polymorphic class. Without it you can't runtime check if your casting is OK :).
After it you can write: