Re: Can you spot anything wrong with this class/structure?
"Ian Collins" <ian-news@hotmail.com> wrote in message
news:5readoF13dm7mU20@mid.individual.net...
Jim Langston wrote:
Okay, how does this look?
struct JVEC2
{
JVEC2(float x=0,float y=0): x(x), y(y) {}
// Supported operators
JVEC2 operator+(JVEC2 a) const { return JVEC2( x + a.x, y + a.y ); }
JVEC2 operator-(JVEC2 a) const { return JVEC2( x - a.x, y - a.y ); }
JVEC2 operator*(JVEC2 a) const { return JVEC2( x * a.x, y * a.y ); }
JVEC2 operator/(JVEC2 a) const { return JVEC2( x / a.x, y / a.y ); }
These should take a const reference as well.
JVEC2 operator+(float a) const { return JVEC2( x + a, y + a ); }
JVEC2 operator-(float a) const { return JVEC2( x - a, y - a ); }
JVEC2 operator*(float a) const { return JVEC2( x * a, y * a ); }
JVEC2 operator/(float a) const { return JVEC2( x / a, y / a ); }
JVEC2& operator+=(const JVEC2& a) {x += a.x;y += a.y;return *this;}
JVEC2& operator*=(const JVEC2& a) {x *= a.x;y *= a.y;return *this;}
JVEC2& operator-=(const JVEC2& a) {x -= a.x;y -= a.y;return *this;}
JVEC2& operator/=(const JVEC2& a) {x /= a.x;y /= a.y;return *this;}
JVEC2& operator+=(float a) {x += a;y += a;return *this;}
JVEC2& operator-=(float a) {x -= a;y -= a;return *this;}
JVEC2& operator*=(float a) {x *= a;y *= a;return *this;}
JVEC2& operator/=(float a) {x /= a;y /= a;return *this;}
bool operator == (JVEC2 a) const {if (x == a.x && y == a.y) return
true;
else return false;}
bool operator != (JVEC2 a) const {if (x != a.x || y != a.y) return
true;
else return false;}
So should these. A cleaner form would be
bool operator==( const JVEC2& a) const {return (x == a.x && y == a.y); }
Ooops, you're right, missed those.
struct JVEC2
{
JVEC2(float x=0,float y=0): x(x), y(y) {}
// Supported operators
JVEC2 operator+(const JVEC2& a) const { return JVEC2( x + a.x, y +
a.y ); }
JVEC2 operator-(const JVEC2& a) const { return JVEC2( x - a.x, y -
a.y ); }
JVEC2 operator*(const JVEC2& a) const { return JVEC2( x * a.x, y *
a.y ); }
JVEC2 operator/(const JVEC2& a) const { return JVEC2( x / a.x, y /
a.y ); }
JVEC2 operator+(float a) const { return JVEC2( x + a, y + a ); }
JVEC2 operator-(float a) const { return JVEC2( x - a, y - a ); }
JVEC2 operator*(float a) const { return JVEC2( x * a, y * a ); }
JVEC2 operator/(float a) const { return JVEC2( x / a, y / a ); }
JVEC2& operator+=(const JVEC2& a) {x += a.x;y += a.y;return *this;}
JVEC2& operator*=(const JVEC2& a) {x *= a.x;y *= a.y;return *this;}
JVEC2& operator-=(const JVEC2& a) {x -= a.x;y -= a.y;return *this;}
JVEC2& operator/=(const JVEC2& a) {x /= a.x;y /= a.y;return *this;}
JVEC2& operator+=(float a) {x += a;y += a;return *this;}
JVEC2& operator-=(float a) {x -= a;y -= a;return *this;}
JVEC2& operator*=(float a) {x *= a;y *= a;return *this;}
JVEC2& operator/=(float a) {x /= a;y /= a;return *this;}
bool operator == (const JVEC2& a) const {if (x == a.x && y == a.y)
return true; else return false;}
bool operator != (const JVEC2& a) const {if (x != a.x || y != a.y)
return true; else return false;}
JVEC2 operator + () const {return JVEC2( +x, +y ); }
JVEC2 operator - () const {return JVEC2( -x, -y ); }
// data
float x,y;
};