Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deriving from a template

I'm stuck on the following and could use some help:

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...
}

It looks like the derived class is not getting the overloaded operator

like image 732
kberson Avatar asked Mar 15 '26 02:03

kberson


1 Answers

Try this:

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

The implicit Evolved::operator=(const Evovled&) that is provided for you hides all instances of operator= present in the base class. (This is true of any method - methods of derived classes hide similarly-named methods of base class, even if the signatures don't match.)

like image 123
Robᵩ Avatar answered Mar 16 '26 16:03

Robᵩ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!