C++接口(interface)类导致函数调用不明确(Qt)

标签 c++ qt interface

这可能是因为我不完全了解 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 继承自BasePropertyIProperty,它们都有相同的基类IBaseProperty。这就是为什么存在歧义。

关于C++接口(interface)类导致函数调用不明确(Qt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17404196/

相关文章:

c++ - Abstract 和 Derived 与 std::list 结合

c++ - 将 const Key_Type& 传递给 std::map 的 operator[]

c++ - 如何在 QTableView 中禁用编辑模式?

c++ - C++ 中的迭代器类别

java - 引用类型和对象类型之间的区别?

c++ - RValue、模板解析和复制构造函数(在 Visual C++ 2010 中)

c++ - 如何在vscode扩展名中检索C/C++文件的编译命令

c++ - Ubuntu CMake 什么路径添加到 CMAKE_MODULE_PATH

c++ - QSettings : is there a limit in . ini文件行长度?

java - 为什么继承类静态方法而不是接口(interface)静态方法?