I was reading this document for templates:
An Idiot's Guide to C++ Templates - Part 2
I came across one definition that I did not understand:
struct Currency
{
int Dollar;
int Cents;
operator double()
{
return Dollar + (double)Cents/100;
}
};
It involves the definition of the operator double() function inside the struct body.
What exactly is this, and what does it do?
At first, I thought this was a functor, but a functor definition looks like this:
int operator ()
The
operator double()makes an instance ofCurrencyimplicitly convertible todouble. Here are two examples whereoperator double()is used: