You must do this:
template <class T2> Number<T>& operator=( const Number<T2>& number ) { m_value = number.value(); return *this; }
That is, use T2
in the parameter type, not the return type!
I would prefer to use a different letter for the template parameter:
template <class U> Number<T>& operator=( const Number<U>& number ) { m_value = number.m_value;
I think it is better to use explicit casting if you want to use the class type as an argument to a template and whose constructor has been declared explicit
:
m_value = static_cast<T>(number.m_value);
By the way, another operator=
should be implemented as:
Number<T>& operator=(T const & value )
source share