0

I want to make a type to differentiate degrees and radians. I wanted something like this to produce a compiler error:

#include <iostream>

typedef double Degree;
typedef double Radian;

void MyFunction(Radian angle);

int main()
{
  Degree foo = 3.0;
  
  return 0;
}

void MyFunction(Radian angle)
{
  std::cout << "Success." << std::endl; 
}

But the typedef just translates directly to "double" so there is no problem passing a Degree to a function expecting a Radian.

I could make a struct and do

Radian foo;
foo.value = 3;
MyFunction(foo);
...
void MyFunction(Radian r);

But then I always have this ".value" hanging around everywhere. Is there no way to do what I'm looking to do?

Thanks,

Dave

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Narue
0

How about two wrappers with implicit conversions?

class Radian {
  double value;
public:
  Radian ( double init ): value ( init ) {}
  operator double() { return value; }
};

class Degree {
  double value;
public:
  Degree ( double init ): value ( init ) {}
  operator double() { return value; }
};
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.