这是我要完成的工作:
我正在尝试创建各种类型的链接列表。为了实现这一点,我认为多态性将是一个很好的方法。
我有两个类,AttributeBase 和 Attribute。AttributeBase 由 AttributeSet 使用,它只存储 Attribute<T> 的链表(作为 AttributeBase* 的)的起点和终点,并对链表进行修改。AttributeBase 是 Attribute<T> 的基类,仅在设计中用于制作通用指针。当然,Attribute<T> 是存储实际值的特定类型的 AttributeBase。每个 Attribute<T> 的主要数据是一个继承的字符串(属性的名称,或者“键”,如果你愿意的话)和一个 T 类型的值。
所以,到目前为止我有(简化):
class AttributeBase
{
public:
AttributeBase() = delete;
AttributeBase* GetNext() { return next; };
AttributeBase* GetPrev() { return prev; };
std::string GetName() { return name; };
//Sometimes I need to get/set the value stored in a derived class
//But, how would I define the function here since the return
//type is of type T as defined in Attribute?
virtual ???? GetValue = 0;
virtual void SetValue(????) = 0;
friend class AttributeSet;
private:
AttributeBase* next = nullptr;
AttributeBase* prev = nullptr;
std::string name;
};
template <class T>
class Attribute : public AttributeBase
{
public:
Attribute( std::string _name, T _value ){ name = _name; value = _value };
T GetValue(){ return value; };
void Setvalue(T){ value = T; };
private:
T value;
};
class AttributeSet
{
public:
template <class T>
void Add(std::string,T); //Add an Attribute<T>(std::string,T) to the list
void Delete(std::string);
bool Contains(std::string _name); //Scan the list to determine if an
//attribute with name of _name exists
template <class T>
T Get(std::string); //Scan the list for 'name' and return
//AttributeBase*->GetValue()
private:
AttributeBase* start = nullptr;
AttributeBase* end = nullptr;
}
由于我试图保持 AttributeBase 通用和非模板化(以避免 AttributeSet 中的强类型开始和结束指针),这就带来了一个问题。如何为虚函数 BaseAttribute::GetValue() 指定一个尚未指定的返回类型。我第一次尝试使用auto
,得到一个编译错误。
由于没有实际创建 AttributeBase 实例(并且删除了默认构造函数),我认为可以省略 GetValue 并在派生类中定义它。但是,如果我尝试 *AttributeBase->GetValue() 它会出错,因为 GetValue() 没有在 AttributeBase 中定义,只有子类。您会认为编译器会知道指针必须指向派生类(唯一的派生类型),因为 AttributeBase 不能直接构造。
因此,为了使用 GetValue(),我必须提前知道前一个值的类型,才能将 AttributeBase* 转换为 Attribute*。如果 AttributeBase 本身被模板化并包含值 T 类型,这将是微不足道的。然后我可以访问 AttributeBase*->type 来确定我需要转换的指针类型。但是,就像我说的那样,模板化 AttributeBase 破坏了对象的预期用途。
很有可能,我会以一种完全错误的方式来处理这个问题(再一次)。但在这一点上,我被困住了。任何帮助,将不胜感激!