I have 2 classes, each can be converted into other. When I just write for example a = b it compiles and converts as expected but when I do for example a = (OtherClass)b it throws me C2440 compile error. Anyone has idea why this happens?
This is what C2440 error says: "retyping: can't convert from Kelvin to Celsius"(and then "from Celsius to Kelvin")
#define ValueOperator(VALUETYPE) operator VALUETYPE() const { return this->value; }
using Value = double;
//this is in separated file
class Celsius;
class Kelvin
{
public:
Value value;
ValueOperator(Value);
Kelvin();
Kelvin(Value value);
operator Celsius() const;
};
//this is in separated file
class Kelvin;
class Celsius
{
public:
Value value;
ValueOperator(Value);
Celsius();
Celsius(Value value);
operator Kelvin() const;
};
int main()
{
Celsius c = 50;
Kelvin k = 10;
c = k; //converts value as excepted
c = (Celsius)k; //error C2440
k = c; //converts value as excepted
k = (Kelvin)c; //error C2440
return 0;
}