I have defined a class A, the actual properties are irrelevant. Is it possible to define a specialization of the static_cast<class T>(int) operator to convert from integers to class A?
So far I have been doing this by defining a convert function, such as A convert(int). But I would rather use static_cast for consistency with other conversions.
Is it possible?
I also want to avoid implicit conversions, which is why I’m not doing this through A‘s constructor.
static_castis a language keyword and you can’t do anything to change that. However you can achieve what you what with an explicit converting constructor:This works because the language defines
static_cast‘s behavior in terms of object construction, invoking conversions as appropriate.