3

私は次のことで立ち往生しており、いくつかの助けを借りることができます:

typedef unsigned short USHORT;

template <typename DataType>
class Primative
{
protected:
    DataType m_dtValue;
public:
    Primative() : m_dtValue(0) {}

    DataType operator=(const DataType c_dtValue) { return m_dtValue = c_dtValue; }
    DataType Set(const DataType c_dtValue){ return m_dtValue = c_dtValue; }
};

typedef Primative<USHORT> PrimativeUS;

class Evolved : public PrimativeUS
{
public:
    Evolved() {}
};

int main()
{
    PrimativeUS prim;
    prim = 4;

    Evolved evo;
    evo.Set(5);  // good
    evo = USHORT(5); // error, no operator found which takes a right-hand operator...
}

派生クラスがオーバーロードされた演算子を取得していないようです

4

2 に答える 2

3

これを試して:

class Evolved : public PrimativeUS
{
public:
  using PrimativeUS::operator=;
  Evolved() {}
};

提供されている暗黙的は、基本クラスEvolved::operator=(const Evovled&)に存在するすべてのインスタンスを非表示にします。operator=(これはどのメソッドにも当てはまります。派生クラスのメソッドは、シグネチャが一致しない場合でも、基本クラスの同様の名前のメソッドを非表示にします。)

于 2011-05-12T16:20:12.027 に答える
1

関数宣言を少し変更します。

DataType operator=(const DataType& c_dtValue) { return m_dtValue = c_dtValue; }
DataType Set(const DataType& c_dtValue){ return m_dtValue = c_dtValue; }

演算子のオーバーロードには & (参照) 記号が必要であることに注意してください。

于 2011-05-12T16:14:38.257 に答える