这可能是因为我不完全了解 C++ 中的接口(interface)是如何工作的,但我们开始吧:
我在 QT5 中有一个属性类的基接口(interface)。
class IBaseProperty
{
public:
virtual ~IBaseProperty() {}
// Returns the property key as a string.
virtual QString getKey() = 0;
// Returns the property value as a raw string.
virtual QString getValueRaw() = 0;
// Sets the property key as a string.
virtual void setKey(QString key) = 0;
// Sets the property value as a raw string.
virtual void setValueRaw(QString value) = 0;
};
我还有一个模板化接口(interface)扩展,可以更轻松地对处理更具体数据类型的属性进行子类化。
template <class T>
class IProperty : public IBaseProperty
{
public:
virtual ~IProperty() {}
// Classifies a property with a Property_t identifier.
virtual Property_t getPropertyType() = 0;
// Returns the property value as the specified type.
// Bool is true if conversion was successful.
virtual T getValue(bool* success) = 0;
// Sets the property value as the specified type.
virtual void setValue(T value) = 0;
// Returns whether the current value can be converted correctly
// to the specified type.
virtual bool canConvert() = 0;
};
我的基本属性(仅实现 IBaseProperty)如下所示:
class BaseProperty : public QObject, public IBaseProperty
{
Q_OBJECT
public:
explicit BaseProperty(QObject *parent = 0, QString key = "", QString value = "");
virtual QString getKey();
virtual QString getValueRaw();
public slots:
virtual void setKey(QString key);
virtual void setValueRaw(QString value);
protected:
QPair<QString, QString> m_Property; // KV pair this property holds.
};
我将其子类化以创建一个字符串属性——显然基本属性只能返回字符串,但我想在字符串/整数/ float 等之间保持相同的函数格式。通过在所有属性中允许 getValue 来获取属性。在这种情况下,GetValue 只是调用 getValueRaw 来返回值。
class StringProperty : public BaseProperty, public IProperty<QString>
{
Q_OBJECT
public:
explicit StringProperty(QObject *parent = 0, QString key = "", QString value = "");
virtual inline Property_t getPropertyType() { return Prop_String; }
virtual QString getValue(bool* success);
virtual bool canConvert();
public slots:
virtual void setValue(QString value);
};
当我实现 getValue 和 setValue 时出现歧义:
inline QString StringProperty::getValue(bool* success)
{
*success = canConvert();
return getValueRaw(); // This line causes the ambiguity.
}
编译器提示:
C2385: Ambiguous access of 'getValueRaw': could be the 'getValueRaw' in base 'BaseProperty' or could be the 'getValueRaw' in base 'IBaseProperty'.
我不完全确定在这种情况下该怎么做——我认为 IBaseProperty 是一个纯虚拟类意味着无论如何都无法从这一点调用该函数,因此只能从它在何处实现(BaseProperty)。解决此问题的正确做法是什么?我不确定应该从哪个基类调用该函数。
最佳答案
乍一看,好像很经典diamond problem or diamond inheritance
String property
继承自BaseProperty
和IProperty
,它们都有相同的基类IBaseProperty
。这就是为什么存在歧义。
关于C++接口(interface)类导致函数调用不明确(Qt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17404196/