class template Convert which can convert itself to any data type
Code:
template<class T>
class Convert
{
T data;
public:
Convert(const T& tData = T()) : data(tData)
{ }
template<class C>
bool IsEqualTo( const C& other ) const
{
return data == other;
}
};
Code:
template<class T>
operator T() const
{
return data;
}
Convert<int> IntData(40);
float FloatData;
double DoubleData;
FloatData = IntData;
DoubleData = IntData;
Code:
Convert<int>::operator<float> float();
Convert<int>::operator<double> double();
) ----------------------------------------------------
Why do we use operator? Is float and double function names below?
Code:
Convert<int>::operator<float> float();
Convert<int>::operator<double> double();
Thanks in advance
Re: class template Convert which can convert itself to any data type
Quote:
Originally Posted by
loves_oi
Code:
template<class T>
operator T() const
{
return data;
}
This doesn't make sense. The cast operator can only be defined as a class member function.
What are you trying to achieve at a higher level?