The problem is when you inherit from a class templated the same as the current class. The inherited type will not replaced as you might expect. For example, Number<int>
will not be replaced with MyInt
for inherited operator +
.
The return value and entry parameter of operator +
is a Number<int>
, not a MyInt
. The inherited class must be able to construct a MyInt
from a Number<int>
. Put below line in MyInt
class:
MyInt(const Number<int> &x) : Number<int>(x) {}
To avoid the extra work, it's better not to inherit from Number
, but instead just put a typedef
for int
:
typedef Number<int> MyInt;
... and then everything else is OK.