446,238 Members | 1,859 Online Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

# Angle class - how is my code ?

 P: n/a Hi there, I've attempted to implement an Angle class. An Angle is a subset of an integer, where the range is [0,360). All other operations should be permitted. The code works, I think... except (for example) a = b + 10; needs to be a = b + (Angle) 10; Could some kind soul comment on my code and show me how it could be done better? class Angle { friend std::ostream& operator<<(std::ostream&, const Angle&) { os << s.theta; return os; } public: Angle() : theta(0) { }; Angle(int t) : theta(t) { }; Angle(const Angle& a) : theta(a) { }; Angle& operator=(const Angle& rhs) { theta = rhs % 360; return *this; } Angle& operator+=(const Angle& a) { theta = (theta + a) % 360; if (theta < 0) theta = 360 - abs(theta); return *this; } Angle& operator-=(const Angle& a) { Angle t = a % 360; theta = theta - t; if (theta < 0) theta = 360 - abs(theta); return *this; } operator int() const { return theta; } private: int theta; }; Angle operator-(const Angle& l, const Angle& r) { Angle a = l; a -= r; return a; } Angle operator+(const Angle& l, const Angle& r) { Angle a = l; a += r; return a; } Many thanks, Darren Jul 22 '05 #1
5 Replies

 P: n/a Darren Grant writes: I've attempted to implement an Angle class. An Angle is a subset of an integer, where the range is [0,360). All other operations should be permitted. The code works, I think... except (for example) a = b + 10; needs to be a = b + (Angle) 10; [snip] The standard work around for that is to make the + operator a friend rather than a member function. Friends are treated better than relatives. Go figure. Jul 22 '05 #2

 P: n/a Darren Grant wrote: Hi there, I've attempted to implement an Angle class. An Angle is a subset of an integer, where the range is [0,360). All other operations should be permitted. The code works, I think... except (for example) a = b + 10; needs to be a = b + (Angle) 10; This is because you have a conversion to int operator. Do you need this conversion operator? I find that these kind of conversions to basic types just get in the way. Normally providing a conversion constructor and the appropriate operator overloads works fine. Could some kind soul comment on my code and show me how it could be done better? #include class Angle { friend std::ostream& operator<<(std::ostream&, const Angle&) { os << s.theta; return os; } It does help to post the actual code :). friend std::ostream& operator<<(std::ostream& os, const Angle&s) { I prefer to avoid friends and you could by doing: std::ostream& operator<<(std::ostream& os, const Angle&s) { os << (int)s; return os; } although a method to access to angle could be useful, e.g get_angle(). public: Angle() : theta(0) { }; This allows theta to be out of range: Angle(int t) : theta(t) { }; If you had a function: int clamp_angle ( int angle ) { if ( angle >= 0 ) return angle % 360; else return 360 - (angle % 360); } then you could put: Angle(int t) : theta(clamp_angle(t)) { }; Angle(const Angle& a) : theta(a) { }; Angle& operator=(const Angle& rhs) { theta = rhs % 360; If angles are always in the range [0,360) why take the modulo here? return *this; } Angle& operator+=(const Angle& a) { theta = (theta + a) % 360; if (theta < 0) theta = 360 - abs(theta); return *this; } Once again if angles are always positive how can the result be less that 0? Angle& operator+=(const Angle& a) { theta = (theta + a) % 360; return *this; } Angle& operator-=(const Angle& a) { Angle t = a % 360; theta = theta - t; if (theta < 0) theta = 360 - abs(theta); return *this; } You could do it more efficiently like this: Angle& operator-=(const Angle& a) { theta -= a; if (theta < 0) theta += 360; return *this; } operator int() const { return theta; } I am not a big fan of these operators. private: int theta; }; Angle operator-(const Angle& l, const Angle& r) { Angle a = l; a -= r; return a; } Angle operator+(const Angle& l, const Angle& r) { Angle a = l; a += r; return a; } Many thanks, Darren Jul 22 '05 #3 