简体   繁体   中英

defining and calling conversion operator of a class

I'm reading the article at :

http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1

and i don't understand some point. OKAY .How can "Convert" convert itself to any data type? What does the float() and double() mean at the end of lines below ?? How can converting happen?Could you please explain it with details?

Convert<int>::operator<float> float();
Convert<int>::operator<double> double();

For those of you who wants related part of the article , I'm copy pasting it below and the related part to my question is at the end .

For sure, 'explicit template argument specification' with method template is also possible. Consider another example:

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

Which can be utilized as:

Convert<int> Data;
float Data2 = 1 ;

bool b = Data.IsEqualTo(Data2);

It instantiates Convert::IsEqualTo with float parameter. Explicit specification, as given below, would instantiate it with double:

bool b = Data.IsEqualTo<double>(Data2);

One of the astounding thing, with the help of templates, you can do it by defining conversion operator on top of template!

template<class T>
operator T() const
{
    return data;
} 

It would make possible to convert the Convert' class template instance into any type, whenever possible. Consider following usage example:

Convert<int> IntData(40);
float FloatData;
double DoubleData;


FloatData = IntData;
DoubleData = IntData;

Which would instantiate following two methods (fully qualified names):

Convert<int>::operator<float> float();
Convert<int>::operator<double> double();

On one hand it provides good flexibility, since without writing extra code, Convert can convert itself (the specific instantiation) to any data-type - whenever conversion is possible at compilation level. If conversion is not possible, like from double to string-type, it would raise an error.


A class type can have user-defined conversions . This allows you to convert an instance of the class into another type. For example, here we have a struct that when converted to an int always has the value 42:

struct A {
  operator int() { return 42; }
};

This is a special function, operator int , that is called for conversion to an int. Now if we have an instance of A, a , we can easily convert it to an int:

A a;
int x = a; // x now has the value 42

In the article, they are showing how you can use templates to generate a conversion operator for any type. The function template is defined as:

template<class T>
operator T() const
{
  return data;
}

Now, for whenever you try to convert your object into another type, a new version of that function will be generated, replacing T with the type that you're converting to. So if we do float f = a , the following function will generated:

operator float() const
{
  return data;
}

This is a function that returns the internal data as a float.

The two lines in the article that are confusing you are just showing you which functions would be generated by the example:

Convert<int>::operator<float> float();
Convert<int>::operator<double> double();

The first is operator float that converts to a float. The second is operator double that converts to a double.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM