你能发现这个类/结构有什么问题吗? [英] Can you spot anything wrong with this class/structure?

查看:48
本文介绍了你能发现这个类/结构有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MSVC ++ .net 2003的游戏引擎并没有问题。一些用户使用相同的引擎崩溃时使用相同的引擎崩溃的
$
结构的副本是返回变量。我没有访问代码那么多b $ b崩溃,但开发人员一直在更改返回

结构的函数,因为这个问题返回其他东西。


就个人而言,我没有看到这个结构有什么问题,无法想象为什么它会在DevC ++中崩溃。


除了在宏观之外使用的所有大写字母,不管怎么说,你能发现这个结构有什么不对吗?它对我来说看起来非常合法。

。 (我知道构造函数应该使用初始化列表,而且我已建议使用
)。


struct JVEC2

{

JVEC2(浮动x = 0,浮动y = 0){this-> x = x; this-> y = y;}

//支持运算符

JVEC2运算符+(JVEC2 a)const {JVEC2 temp; temp.x = x + ax; temp.y = y +

ay; return temp;}

JVEC2运算符 - (JVEC2 a)const {JVEC2 temp; temp.x = x - ax; temp.y = y -

ay; return temp;}

JVEC2运算符*(JVEC2 a)const {JVEC2 temp; temp.x = x * ax; temp.y = y *

ay; return temp;}

JVEC2运算符/(JVEC2 a)const {JVEC2 temp; temp.x = x / ax; temp.y = y /

ay; return temp;}

JVEC2运算符+(浮点数a)const {JVEC2 temp; temp.x = x + a; temp.y = y +

a;返回temp;}

JVEC2运算符 - ( 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运算符/(float a)const {JVEC2 temp; temp.x = x / a; temp.y = y /

a; return temp;}

JVEC2运算符+ =(JVEC2 a){x + = ax; y + = ay; return * this;}

JVEC2运算符* =(JVEC2 a){x * = ax; y * = ay; return * this;}

JVEC2运算符 - =(JVEC2 a){x - = ax; y - = ay; return * this;}

JVEC2运算符/ =(JVEC2 a){x / = ax; y / = ay; return * this;}

JVEC2运算符+ =(浮点数a ){x + = a; y + = a; return * this;}

JVEC2运算符 - =(浮动a){x - = a; y - = a; return * this;}

JVEC2算子* =(浮动a){x * = a; y * = a;返回* this;}

JVEC2算子/ =(浮动a){x / = a; y / = a; return * this;}

bool operator ==(JVEC2 a)const {if(x == ax&& y == ay)返回true;

else返回false;}

bool operator!=(JVEC2 a)const {if(x!= ax || y!= ay)返回true;

else返回false;}

JVEC2运算符+()const {JVEC2 t; tx = + x; ty = + y; return t;}

JVEC2运算符 - ()const {JVEC2 t; tx = -x; ty = -y; return t;}

//数据

float x,y;

};

解决方案

Jim Langston写道:


我使用MSVC ++ .net 2003的游戏引擎并没有问题。



听起来不是真的,因为你发布了这个......


当这个结构的

副本是返回变量时,DevC ++的一些用户使用相同的引擎崩溃。我没有访问

到崩溃的代码,但开发人员一直在更改

函数返回结构以返回其他内容,因为

这个问题。

就个人而言,我没有看到这个结构有什么问题,并且不能想象为什么它会在DevC ++中崩溃。



DevC ++是一个IDE。结构如何崩溃IDE?


无论如何,如果没有看到崩溃的代码,我只能提出几个建议,见下文。


除了在宏观之外使用的所有大写字母,这个结构是什么?你能看到这个结构的错误吗?它看起来很好,对我来说是完全合法的。 (我知道构造函数应该使用

初始化列表而且我已经建议了。)


struct JVEC2

{

JVEC2(浮动x = 0,浮动y = 0){this-> x = x; this-> y = y;}



哎呀。


//支持的运算符

JVEC2运算符+(JVEC2 a)const {JVEC2 temp; temp.x = x + ax; temp.y

= y + ay;返回temp;}



呃...... Yechhh!以下是您需要遵循的模式:

JVEC2运算符+(JVEC2 const& a)const {

返回JVEC2(x + ax,y + ay) ;

}


重写所有非复合运算符。


JVEC2运算符 - (JVEC2 a)const {JVEC2 temp; temp.x = x - ax; temp.y

= y - ay; return temp;}

JVEC2运算符*(JVEC2 a) const {JVEC2 temp; temp.x = x * ax; temp.y

= y * ay; return temp;}

JVEC2 operator /(JVEC2 a)const { JVEC2 temp; temp.x = x / ax; temp.y

= y / ay; return temp;}

JVEC2运算符+(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运算符*(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运算符+ =(JVEC2 a){x + = ax; y + = ay; return * this;}



呃......复合作业应该返回*参考*!并且

他们应该参考const!


JVEC2算子* =(JVEC2 a){x * = ax; y * = ay; return * this;}

JVEC2运算符 - =(JVEC2 a){x - = ax; y - = ay; return * this;}

JVEC2运算符/ =(JVEC2 a){x / = ax; y / = ay; return * this;}

JVEC2运算符+ =(浮动a){x + = a; y + = a;返回* this ;}

JVEC2运算符 - =(浮点数a){x - = a; y - = a; return * this;}

JVEC2运算符* =(浮点数a) {x * = a; y * = a; return * this;}

JVEC2 operator / =(float a){x / = a; y / = a; return * this;}

bool operator ==(JVEC2 a)const {if(x == ax&& y == ay)return

true;否则返回false;}

bool operator!=(JVEC2 a)const {if(x!= a.x || y!= a.y)return

true; else返回false;}

JVEC2运算符+()const {JVEC2 t; tx = + x; ty = + y; return t;}

JVEC2运算符 - () const {JVEC2 t; tx = -x; ty = -y; return t;}



Yikes!同样在这里


JVEC2运算符+()const {return * this; } //一元加上确实

//没有任何花车


JVEC2运算符 - ()const {return JVEC2(-x,-y); }


//数据

浮动x,y;

};



V

-

请在通过电子邮件回复时删除资金'A'

我没有回复最热门的回复,请不要问




" Victor巴札罗夫" < v。******** @ comAcast.netwrote in message

news:Ec ********************* *********@comcast.com。 ..


Jim Langston写道:


>我使用MSVC ++ .net 2003的游戏引擎并没有问题。



听起来不是真的,因为你发布了这个...



我对代码没有任何问题,但是当可执行文件崩溃时,使用DevC ++编译的人会遇到问题。


> DevC ++的一些用户使用相同的引擎,当这个结构的副本是返回变量时会崩溃。我无法访问正在崩溃的代码,但是开发人员一直在更改返回结构的函数,因为这个问题会回复。
就个人而言,我没有看到这个结构有什么问题,也无法想象为什么它会在DevC ++中崩溃。



DevC ++是一个IDE。结构如何崩溃IDE?


无论如何,如果没有看到崩溃的代码,我只能提出几个建议,见下文。


>除了在一个令人皱眉的宏之外使用的所有大写字母外,你能发现这个结构有什么问题吗?看起来对我来说是完全合法的。 (我知道构造函数应该使用初始化列表并且我已经建议了)。

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



哎哟。



是的,我同意。一直想让他改变这个。


> //支持的运算符
JVEC2运算符+(JVEC2 a)const {JVEC2 temp; temp.x = x + ax; temp.y
= y + ay; return temp;}


呃...... Yechhh!以下是您需要遵循的模式:

JVEC2运算符+(JVEC2 const& a)const {

返回JVEC2(x + ax,y + ay) ;

}



嗯......既然他有构造函数,是的,他可以做到这一点。它woudl

简化代码,但不知道它是否可以防止崩溃。


重写所有非复合运算符。


> JVEC2运算符 - (JVEC2 a)const {JVEC2 temp; temp.x = x - ax; temp.y
= y - ay; return temp;}
JVEC2运算符*(JVEC2 a)const {JVEC2 temp; temp.x = x * ax; temp.y
= y * ay; return temp;}
JVEC2 operator /(JVEC2 a)const {JVEC2 temp; temp.x = x / ax; temp.y
= y / ay; return temp;}
JVEC2 operator +(float a)const {JVEC2 temp; temp.x = x + a; temp.y =
y + a ;返回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运算符+ =(JVEC2 a){x + = ax; y + = ay; return * this ;}



呃......复合作业应该返回*参考*!并且

他们应该参考const!



他们会返回什么参考?如果它是临时的,那不是吗?b $ b消失了吗?哪个是对的:

JVEC2& operator-(const JVEC2 a){x = x - a.x; y = y - a.y;返回此;}



JVEC2& operator-(const JVEC2 a)const {JVEC2 temp; temp.x = x - ax; temp.y

= y - ay; return temp;}


> JVEC2运算符* =(JVEC2 a){x * = ax; y * = ay; return * this;}
JVEC2运算符 - =(JVEC2 a){x - = ax; y - = ay; return * this ;}
JVEC2运算符/ =(JVEC2 a){x / = ax; y / = ay; return * this;}
JVEC2运算符+ =(float a){x + = a; y + = a; return * this;}
JVEC2运算符 - =(float a){x - = a; y - = a; return * this;}
JVEC2运算符* =(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 == ax&& y == ay)return
true; else返回false;}
bool operator!=(JVEC2 a)const {if(x!= a.x || y!= a.y)return
true; else返回false;}
JVEC2运算符+()const {JVEC2 t; tx = + x; ty = + y; return t;}
JVEC2运算符 - ()const {JVEC2 t; tx = - x; ty = -y; return t;}



Yikes!同样在这里


JVEC2运算符+()const {return * this; } //一元加上确实

//没有任何花车


JVEC2运算符 - ()const {return JVEC2(-x,-y); }


> // data
float x,y;
};



V



" Victor Bazarov" < v。******** @ comAcast.netwrote in message

news:Ec ********************* *********@comcast.com。 ..


Jim Langston写道:


>我使用MSVC ++ .net 2003的游戏引擎并没有问题。



听起来不是真的,因为你发布了这个......


> DevC ++的一些用户使用相同的引擎时,有时这个结构的副本是返回变量。我无法访问正在崩溃的代码,但是开发人员一直在更改返回结构的函数,因为这个问题会回复。
就个人而言,我没有看到这个结构有什么问题,也无法想象为什么它会在DevC ++中崩溃。



DevC ++是一个IDE。结构如何崩溃IDE?


无论如何,如果没有看到崩溃的代码,我只能提出几个建议,见下文。


>除了在一个令人皱眉的宏之外使用的所有大写字母外,你能发现这个结构有什么问题吗?看起来对我来说是完全合法的。 (我知道构造函数应该使用初始化列表并且我已经建议了)。

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



Ouch。


> //支持的运算符
JVEC2运算符+(JVEC2 a)const {JVEC2 temp; temp.x = x + ax; temp.y
= y + ay; return temp;}


呃...... Yechhh!以下是您需要遵循的模式:

JVEC2运算符+(JVEC2 const& a)const {

返回JVEC2(x + ax,y + ay) ;

}


重写所有非复合运算符。


> JVEC2运算符 - (JVEC2 a)const {JVEC2 temp; temp.x = x - ax; temp.y
= y - ay; return temp;}
JVEC2运算符*(JVEC2 a)const {JVEC2 temp; temp.x = x * ax; temp.y
= y * ay; return temp;}
JVEC2 operator /(JVEC2 a)const {JVEC2 temp; temp.x = x / ax; temp.y
= y / ay; return temp;}
JVEC2 operator +(float a)const {JVEC2 temp; temp.x = x + a; temp.y =
y + a ;返回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运算符+ =(JVEC2 a){x + = ax; y + = ay; return * this ;}



呃......复合作业应该返回*参考*!并且

他们应该参考const!


> JVEC2运算符* =(JVEC2 a){x * = ax; y * = ay; return * this;}
JVEC2运算符 - =(JVEC2 a){x - = ax; y - = ay; return * this ;}
JVEC2运算符/ =(JVEC2 a){x / = ax; y / = ay; return * this;}
JVEC2运算符+ =(float a){x + = a; y + = a; return * this;}
JVEC2运算符 - =(float a){x - = a; y - = a; return * this;}
JVEC2运算符* =(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 == ax&& y == ay)return
true; else返回false;}
bool operator!=(JVEC2 a)const {if(x!= a.x || y!= a.y)return
true; else返回false;}
JVEC2运算符+()const {JVEC2 t; tx = + x; ty = + y; return t;}
JVEC2运算符 - ()const {JVEC2 t; tx = - x; ty = -y; return t;}



Yikes!同样在这里


JVEC2运算符+()const {return * this; } //一元加上确实

//没有任何花车


JVEC2运算符 - ()const {return JVEC2(-x,-y); }


> // data
float x,y;
};



好​​的,这看起来怎么样?

struct JVEC2

{

JVEC2(浮动x = 0,浮动y = 0):x(x),y​​(y){}

//支持的运算符

JVEC2运算符+(JVEC2 a)const {return JVEC2(x + ax,y + ay); } $ / $
JVEC2运算符 - (JVEC2 a)const {return JVEC2(x - a.x,y - a.y); } $ / $
JVEC2运算符*(JVEC2 a)const {return JVEC2(x * a.x,y * a.y); } $ / $
JVEC2运算符/(JVEC2 a)const {return JVEC2(x / a.x,y / a.y); } $ / $
JVEC2运算符+(float a)const {return JVEC2(x + a,y + a); } $ / $
JVEC2 operator-(float a)const {return JVEC2(x - a,y - a); } $ / $
JVEC2运算符*(float a)const {return JVEC2(x * a,y * a); } $ / $
JVEC2运算符/(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 == ax&& ; y == ay)返回true;

else返回false;}

bool operator!=(JVEC2 a)const {if(x!= ax || y! = ay)返回true;

else返回false;}

JVEC2运算符+()const {return JVEC2(+ x,+ y); } $ / $
JVEC2运算符 - ()const {return JVEC2(-x,-y); }

//数据

浮动x,y;

};



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;
};

解决方案

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



"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



"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;
};



这篇关于你能发现这个类/结构有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆