これは、C++ のインターフェイスがどのように機能するかを完全に理解していないことが原因である可能性がありますが、次のようになります。
QT5 のプロパティ クラスの基本インターフェイスがあります。
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;
};
また、より具体的なデータ型を処理するプロパティを簡単にサブクラス化できるように、テンプレート化されたインターフェイス拡張も用意しています。
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.
};
これをサブクラス化して文字列プロパティを作成します。明らかに基本プロパティは文字列を返すだけですが、string/int/float/etc 間で同じ関数形式を維持したかったのです。それらのすべてで 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: 'getValueRaw' のあいまいなアクセス: ベース 'BaseProperty' の 'getValueRaw' であるか、ベース 'IBaseProperty' の 'getValueRaw' である可能性があります。
この状況で何をすべきか完全にはわかりません.IBasePropertyが純粋な仮想クラスであるということは、とにかくこの時点から関数を呼び出すことができないことを意味すると考えていたので、それがあった場所からのみ呼び出されます.実装されています (BaseProperty)。これを修正するために取るべき正しい行動方針は何でしょうか? どの基本クラスから関数を呼び出す必要があるかわかりません。