#ifndef NUMBER_HPP
#define NUMBER_HPP
template <class T>
class Number
{
public:
Number( T value ) : m_value( value )
{
}
T value() const
{
return m_value;
}
void setValue( T value )
{
m_value = value;
}
Number<T>& operator=( T value )
{
m_value = value;
}
// template <class T2>
// Number<T2>& operator=( const Number<T>& number )
// {
// m_value = number.value();
// return *this;
// }
private:
T m_value;
};
typedef Number<int> Integer;
typedef Number<float> Float;
typedef Number<double> Double;
#endif // NUMBER_HPP
The commented assignment operator overloading is my attempt to do what I want, I thought it might provide a better description than the one above the snippet.
I want to be able to do the following:
Float a(10);
Integer b(20);
a = b;
Where a then would be casted to an int and given the value of b, but still be an instance of class Number.
Is it possible? Can you help me out here?
Thanks in advance.
You should do this:
That is, use
T2in the parameter type, not in the return type!I would rather use different letter for template parameter:
I think, it is better to use explicit cast, if you want to use class type as template argument and whose constructor has been declared
explicit:By the way, the other
operator=should be implemented as: