I've been playing around with C++ and I've noticed something that I don't quite understand:
typedef float degrees;
typedef float radians;
void my_func(degrees n);
void my_func(radians m);
Declaring a function like this, I get a warning that the function is re-declared as if they are identical. Does this mean, when looking at function definitions, the compiler only sees built-in types and doesn't care about custom defined types, and since they're bot floats, it just considers them to be the same function?...
If that's the case, how do I get around this? Do I just have to make a different function?
Another possibility is to define
Radian
andDegree
classes to have an explicit conversion fromfloat
s and an implicit conversion to them.The implicit conversions to
float
mean that you can pass a variable of typeRadian
orDegree
to a function expectingfloat
and it'll just work.This version does mean that, unlike the
typedef
s, you won't be able to write things likeHowever, if you want, you can also define arithmetic operators like
operator+
,operator*
, etc for each class. For instance, you might want to always perform Degree arithmetic modulo360
, so that180 + 180 = 0
.