473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can you spot anything wrong with this class/structure?

I use a game engine using MSVC++ .net 2003 and have no problems. Some users
of DevC++ who use the same engine crash at times when a copy of this
structure is the return variable. I don't have access to the code that is
crashing, but the developer has been changing functions that return the
structure to return something else because of this problem.

Personally, I don't see anything wrong with this structure and can't imagine
why it would be crashing in DevC++.

Aside from the all caps used outside of a macro which is frowned upon, can
you spot anything wrong with this strucutre? It looks perfectly legitimite
to me. ( I know the consructor should be using an initializer list and I've
suggested that ).

struct JVEC2
{
JVEC2(float x=0,float y=0){this->x=x;this->y=y;}
// Supported operators
JVEC2 operator+(JVEC2 a) const {JVEC2 temp;temp.x = x + a.x;temp.y = y +
a.y;return temp;}
JVEC2 operator-(JVEC2 a) const {JVEC2 temp;temp.x = x - a.x;temp.y = y -
a.y;return temp;}
JVEC2 operator*(JVEC2 a) const {JVEC2 temp;temp.x = x * a.x;temp.y = y *
a.y;return temp;}
JVEC2 operator/(JVEC2 a) const {JVEC2 temp;temp.x = x / a.x;temp.y = y /
a.y;return temp;}
JVEC2 operator+(float a) const {JVEC2 temp;temp.x = x + a;temp.y = y +
a;return temp;}
JVEC2 operator-(float a) const {JVEC2 temp;temp.x = x - a;temp.y = y -
a;return temp;}
JVEC2 operator*(float a) const {JVEC2 temp;temp.x = x * a;temp.y = y *
a;return temp;}
JVEC2 operator/(float a) const {JVEC2 temp;temp.x = x / a;temp.y = y /
a;return temp;}
JVEC2 operator+=(JVEC2 a) {x += a.x;y += a.y;return *this;}
JVEC2 operator*=(JVEC2 a) {x *= a.x;y *= a.y;return *this;}
JVEC2 operator-=(JVEC2 a) {x -= a.x;y -= a.y;return *this;}
JVEC2 operator/=(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;}
JVEC2 operator + () const {JVEC2 t;t.x=+x;t.y=+y;return t;}
JVEC2 operator - () const {JVEC2 t;t.x=-x;t.y=-y;return t;}
// data
float x,y;
};
Dec 1 '07 #1
10 1527
Jim Langston wrote:
I use a game engine using MSVC++ .net 2003 and have no problems.
Doesn't really sound like it, since you're posting this...
Some users of DevC++ who use the same engine crash at times when a
copy of this structure is the return variable. I don't have access
to the code that is crashing, but the developer has been changing
functions that return the structure to return something else because
of this problem.
Personally, I don't see anything wrong with this structure and can't
imagine why it would be crashing in DevC++.
DevC++ is an IDE. How can a struct crash an IDE?

Anyway, without seeing the code that does crash, I can only make
a few suggestions, see below.
Aside from the all caps used outside of a macro which is frowned
upon, can you spot anything wrong with this strucutre? It looks
perfectly legitimite to me. ( I know the consructor should be using
an initializer list and I've suggested that ).

struct JVEC2
{
JVEC2(float x=0,float y=0){this->x=x;this->y=y;}
Ouch.
// Supported operators
JVEC2 operator+(JVEC2 a) const {JVEC2 temp;temp.x = x + a.x;temp.y
= y + a.y;return temp;}
Ugh... Yechhh! Here is the pattern you need to follow:

JVEC2 operator+(JVEC2 const& a) const {
return JVEC2(x + a.x, y + a.y);
}

Rewrite all non-compound operators.
JVEC2 operator-(JVEC2 a) const {JVEC2 temp;temp.x = x - a.x;temp.y
= y - a.y;return temp;}
JVEC2 operator*(JVEC2 a) const {JVEC2 temp;temp.x = x * a.x;temp.y
= y * a.y;return temp;}
JVEC2 operator/(JVEC2 a) const {JVEC2 temp;temp.x = x / a.x;temp.y
= y / a.y;return temp;}
JVEC2 operator+(float a) const {JVEC2 temp;temp.x = x + a;temp.y =
y + a;return temp;}
JVEC2 operator-(float a) const {JVEC2 temp;temp.x = x - a;temp.y =
y - a;return temp;}
JVEC2 operator*(float a) const {JVEC2 temp;temp.x = x * a;temp.y =
y * a;return temp;}
JVEC2 operator/(float a) const {JVEC2 temp;temp.x = x / a;temp.y =
y / a;return temp;}
JVEC2 operator+=(JVEC2 a) {x += a.x;y += a.y;return *this;}
Ugh... Compound assignments should return a *reference*! And
they should take a ref to const!
JVEC2 operator*=(JVEC2 a) {x *= a.x;y *= a.y;return *this;}
JVEC2 operator-=(JVEC2 a) {x -= a.x;y -= a.y;return *this;}
JVEC2 operator/=(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;}
JVEC2 operator + () const {JVEC2 t;t.x=+x;t.y=+y;return t;}
JVEC2 operator - () const {JVEC2 t;t.x=-x;t.y=-y;return t;}
Yikes! Same here

JVEC2 operator +() const { return *this; } // unary plus does
// nothing for floats

JVEC2 operator -() const { return JVEC2(-x, -y); }
// data
float x,y;
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 1 '07 #2

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:Ec******************************@comcast.com. ..
Jim Langston wrote:
>I use a game engine using MSVC++ .net 2003 and have no problems.

Doesn't really sound like it, since you're posting this...
I have no problems with the code, but people who compile with DevC++ have
problems when the executable crashes.
>Some users of DevC++ who use the same engine crash at times when a
copy of this structure is the return variable. I don't have access
to the code that is crashing, but the developer has been changing
functions that return the structure to return something else because
of this problem.
Personally, I don't see anything wrong with this structure and can't
imagine why it would be crashing in DevC++.

DevC++ is an IDE. How can a struct crash an IDE?

Anyway, without seeing the code that does crash, I can only make
a few suggestions, see below.
>Aside from the all caps used outside of a macro which is frowned
upon, can you spot anything wrong with this strucutre? It looks
perfectly legitimite to me. ( I know the consructor should be using
an initializer list and I've suggested that ).

struct JVEC2
{
JVEC2(float x=0,float y=0){this->x=x;this->y=y;}

Ouch.
Yeah, I agree. Been trying to get him to change this.
> // Supported operators
JVEC2 operator+(JVEC2 a) const {JVEC2 temp;temp.x = x + a.x;temp.y
= y + a.y;return temp;}

Ugh... Yechhh! Here is the pattern you need to follow:

JVEC2 operator+(JVEC2 const& a) const {
return JVEC2(x + a.x, y + a.y);
}
Hmm.. now that he has the constructor, yeah, he can do this. It woudl
simplify the code, but don't know if it could prevent a crash.
Rewrite all non-compound operators.
> JVEC2 operator-(JVEC2 a) const {JVEC2 temp;temp.x = x - a.x;temp.y
= y - a.y;return temp;}
JVEC2 operator*(JVEC2 a) const {JVEC2 temp;temp.x = x * a.x;temp.y
= y * a.y;return temp;}
JVEC2 operator/(JVEC2 a) const {JVEC2 temp;temp.x = x / a.x;temp.y
= y / a.y;return temp;}
JVEC2 operator+(float a) const {JVEC2 temp;temp.x = x + a;temp.y =
y + a;return temp;}
JVEC2 operator-(float a) const {JVEC2 temp;temp.x = x - a;temp.y =
y - a;return temp;}
JVEC2 operator*(float a) const {JVEC2 temp;temp.x = x * a;temp.y =
y * a;return temp;}
JVEC2 operator/(float a) const {JVEC2 temp;temp.x = x / a;temp.y =
y / a;return temp;}
JVEC2 operator+=(JVEC2 a) {x += a.x;y += a.y;return *this;}

Ugh... Compound assignments should return a *reference*! And
they should take a ref to const!
What would they return a reference to though? If it's the temp, won't that
disappear? Which is correct:
JVEC2& operator-(const JVEC2 a) {x = x - a.x; y = y - a.y; return this;}
or
JVEC2& operator-(const JVEC2 a) const {JVEC2 temp;temp.x = x - a.x;temp.y
= y - a.y;return temp;}
> JVEC2 operator*=(JVEC2 a) {x *= a.x;y *= a.y;return *this;}
JVEC2 operator-=(JVEC2 a) {x -= a.x;y -= a.y;return *this;}
JVEC2 operator/=(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;}
JVEC2 operator + () const {JVEC2 t;t.x=+x;t.y=+y;return t;}
JVEC2 operator - () const {JVEC2 t;t.x=-x;t.y=-y;return t;}

Yikes! Same here

JVEC2 operator +() const { return *this; } // unary plus does
// nothing for floats

JVEC2 operator -() const { return JVEC2(-x, -y); }
> // data
float x,y;
};

V

Dec 1 '07 #3
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:Ec******************************@comcast.com. ..
Jim Langston wrote:
>I use a game engine using MSVC++ .net 2003 and have no problems.

Doesn't really sound like it, since you're posting this...
>Some users of DevC++ who use the same engine crash at times when a
copy of this structure is the return variable. I don't have access
to the code that is crashing, but the developer has been changing
functions that return the structure to return something else because
of this problem.
Personally, I don't see anything wrong with this structure and can't
imagine why it would be crashing in DevC++.

DevC++ is an IDE. How can a struct crash an IDE?

Anyway, without seeing the code that does crash, I can only make
a few suggestions, see below.
>Aside from the all caps used outside of a macro which is frowned
upon, can you spot anything wrong with this strucutre? It looks
perfectly legitimite to me. ( I know the consructor should be using
an initializer list and I've suggested that ).

struct JVEC2
{
JVEC2(float x=0,float y=0){this->x=x;this->y=y;}

Ouch.
> // Supported operators
JVEC2 operator+(JVEC2 a) const {JVEC2 temp;temp.x = x + a.x;temp.y
= y + a.y;return temp;}

Ugh... Yechhh! Here is the pattern you need to follow:

JVEC2 operator+(JVEC2 const& a) const {
return JVEC2(x + a.x, y + a.y);
}

Rewrite all non-compound operators.
> JVEC2 operator-(JVEC2 a) const {JVEC2 temp;temp.x = x - a.x;temp.y
= y - a.y;return temp;}
JVEC2 operator*(JVEC2 a) const {JVEC2 temp;temp.x = x * a.x;temp.y
= y * a.y;return temp;}
JVEC2 operator/(JVEC2 a) const {JVEC2 temp;temp.x = x / a.x;temp.y
= y / a.y;return temp;}
JVEC2 operator+(float a) const {JVEC2 temp;temp.x = x + a;temp.y =
y + a;return temp;}
JVEC2 operator-(float a) const {JVEC2 temp;temp.x = x - a;temp.y =
y - a;return temp;}
JVEC2 operator*(float a) const {JVEC2 temp;temp.x = x * a;temp.y =
y * a;return temp;}
JVEC2 operator/(float a) const {JVEC2 temp;temp.x = x / a;temp.y =
y / a;return temp;}
JVEC2 operator+=(JVEC2 a) {x += a.x;y += a.y;return *this;}

Ugh... Compound assignments should return a *reference*! And
they should take a ref to const!
> JVEC2 operator*=(JVEC2 a) {x *= a.x;y *= a.y;return *this;}
JVEC2 operator-=(JVEC2 a) {x -= a.x;y -= a.y;return *this;}
JVEC2 operator/=(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;}
JVEC2 operator + () const {JVEC2 t;t.x=+x;t.y=+y;return t;}
JVEC2 operator - () const {JVEC2 t;t.x=-x;t.y=-y;return t;}

Yikes! Same here

JVEC2 operator +() const { return *this; } // unary plus does
// nothing for floats

JVEC2 operator -() const { return JVEC2(-x, -y); }
> // data
float x,y;
};
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 ); }
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;}
JVEC2 operator + () const {return JVEC2( +x, +y ); }
JVEC2 operator - () const {return JVEC2( -x, -y ); }
// data
float x,y;
};


Dec 1 '07 #4
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); }

--
Ian Collins.
Dec 1 '07 #5
"Ian Collins" <ia******@hotmail.comwrote in message
news:5r**************@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;
};
Dec 2 '07 #6
On 2007-12-01 16:42:01 -0800, "Jim Langston" <ta*******@rocketmail.comsaid:
>
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;}
These operators are written a little strangely, if I were writing them, I'd do:
bool operator==(const JVEC2& a) const { return (x == a.x && y == a.y); }
bool operator!=(const JVEC2& a) const { return !operator==(a); }
JVEC2 operator + () const {return JVEC2( +x, +y ); }
For this, I'd just do:
JVEC2 operator + () const { return *this; }
JVEC2 operator - () const {return JVEC2( -x, -y ); }
// data
float x,y;
};

--
Clark S. Cox III
cl*******@gmail.com

Dec 2 '07 #7
Jim Langston wrote:
"Ian Collins" <ia******@hotmail.comwrote:
>> 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.
bool operator == (const JVEC2& a) const {if (x == a.x && y == a.y)
return true; else return false;}
You've still got the superfluous if/else.

--
Ian Collins.
Dec 2 '07 #8
On Dec 2, 12:14 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message
news:Ec******************************@comcast.com. ..
Rewrite all non-compound operators.
JVEC2 operator+=(JVEC2 a) {x += a.x;y += a.y;return *this;}
Ugh... Compound assignments should return a *reference*! And
they should take a ref to const!
What would they return a reference to though?
*this. Because that's what the built-in versions do.

This could even be the source of your problem, e.g. if someone
is using the return value to initialize a reference, either in a
context which doesn't extend the lifetime, or if that reference
is then used to initialize some other references. That results
in undefined behavior; depending on how and when the resulting
reference is used, it might crash, or it might work, depending
on the compiler (and probably compiler options, like the level
of optimization).
If it's the temp, won't that
disappear? Which is correct:
JVEC2& operator-(const JVEC2 a) {x = x - a.x; y = y - a.y; return this;}
or
JVEC2& operator-(const JVEC2 a) const {JVEC2 temp;temp.x = x - a.x;temp..y
= y - a.y;return temp;}
That's not a compound assignment. The binary operators should
return values; the compound assignment operators references to
this.

Given that you have implicit conversions as well, the binary
operators should probably not be members, but rather free
functions, so you can write things like 1.0 + aJvec2 as well as
aJvec2 + 1.0.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 2 '07 #9
On Dec 2, 12:52 am, Ian Collins <ian-n...@hotmail.comwrote:
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.
The object is small enough that it probably doesn't really
matter (but a const reference would be more idiomatic). On the
other hand, they definitly should be free functions, not
members.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 2 '07 #10
In message <Ec******************************@comcast.com>, Victor
Bazarov <v.********@comAcast.netwrites
>Jim Langston wrote:
>I use a game engine using MSVC++ .net 2003 and have no problems.

Doesn't really sound like it, since you're posting this...
>Some users of DevC++ who use the same engine crash at times when a
copy of this structure is the return variable. I don't have access
to the code that is crashing, but the developer has been changing
functions that return the structure to return something else because
of this problem.
Personally, I don't see anything wrong with this structure and can't
imagine why it would be crashing in DevC++.

DevC++ is an IDE. How can a struct crash an IDE?

Anyway, without seeing the code that does crash, I can only make
a few suggestions, see below.
>Aside from the all caps used outside of a macro which is frowned
upon, can you spot anything wrong with this strucutre? It looks
perfectly legitimite to me. ( I know the consructor should be using
an initializer list and I've suggested that ).

struct JVEC2
{
JVEC2(float x=0,float y=0){this->x=x;this->y=y;}

Ouch.
> // Supported operators
JVEC2 operator+(JVEC2 a) const {JVEC2 temp;temp.x = x + a.x;temp.y
= y + a.y;return temp;}

Ugh... Yechhh! Here is the pattern you need to follow:

JVEC2 operator+(JVEC2 const& a) const {
return JVEC2(x + a.x, y + a.y);
}

Rewrite all non-compound operators.
You might also consider rewriting them to use the compound ones, to
guarantee consistency...

JVEC2 operator+(JVEC2 const & a) const
{ JVEC2 temp(*this); temp += a; return temp; }

.... or even rewriting them as friend binary functions, to make both
sides of the operator candidates for implicit conversions, if there are
any...

friend JVEC2 operator+(JVEC2 const & a, JVEC2 const & b)
{ JVEC2 temp(a); temp += b; return temp; }
--
Richard Herring
Dec 4 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
12
by: Fred Pacquier | last post by:
First off, sorry for this message-in-a-bottle-like post... I haven't been able to phrase my questions well enough to get a meaningful answer from Google in my research. OTOH, it is standard...
1
by: Adam Monsen | last post by:
Is there anything wrong with using something like super(type(self), self).f() to avoid having to hardcode a type? For example: class A(object): def f(self): print "in A.f()" class B(A): def...
2
by: keithlackey | last post by:
I'm relatively new to python and I've run into this problem. DECLARING CLASS class structure: def __init__(self, folders = ): self.folders = folders def add_folder(self, folder):
3
by: Roubles | last post by:
Hi All, Here's my problem, I have a bunch of code that passes an allocated object (say obj) to a function, and then dereferences that allocated object when the function returns: foo(obj);...
5
by: SamSpade | last post by:
public static extern int SendMessage( IntPtr hWnd, int Msg, int wParam, IntPtr lParam); --snip SelectionFont.cbSize = Marshal.SizeOf(SelectionFont)
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
7
by: David | last post by:
i think i just realized i'm an idiot. again. (not syntactically correct code... just pieces to illustrate) class StateObject { members like socket, receiveBuffer, receiveBufferSize,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.