Re: Can you spot anything wrong with this class/structure?
On Dec 2, 12:14 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"Victor Bazarov" <v.Abaza...@comAcast.net> wrote in message
news:EcydnclyxKrKR83anZ2dnUVZ_s2tnZ2d@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 thi=
s;}
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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34