Thread overview
Struct and operator overloading
Dec 17, 2003
ssuukk
Dec 17, 2003
Patrick Down
Dec 17, 2003
Sean L. Palmer
Dec 17, 2003
Patrick Down
Dec 17, 2003
Charles
Dec 18, 2003
ssuukk
Strange things with static quasi-constructors in structs
Dec 18, 2003
ssuukk
Dec 18, 2003
J Anderson
December 17, 2003
Sometime ago someone suggested that to make a vector that is located on stack instead of on heap one has to make it struct instead of class. I am trying it just now. The question is though, how to use operator overloading in such cases? Normally if it was a class I would new a return value, but here... hmmm... Can anyone write me an example of how to make add operator for two STRUCT vectors?

tia,

ssuukk

--------------------------------

alias real frReal;

struct frVector
{
	frReal x; frReal y; frReal z;
	
	void set(frReal xx, frReal yy, frReal zz) {
		x=xx; y=yy; z=zz;
	 }
	
	void set(frVector vv) {
		x=vv.x; y=vv.y; z=vv.z;
	 }
	
	frVector opNeg() {
		????????
	}
}

December 17, 2003
This is what you need.

In article <brppgb$2ei0$1@digitaldaemon.com>, ssuukk says...
>
>Sometime ago someone suggested that to make a vector that is located on stack instead of on heap one has to make it struct instead of class. I am trying it just now. The question is though, how to use operator overloading in such cases? Normally if it was a class I would new a return value, but here... hmmm... Can anyone write me an example of how to make add operator for two STRUCT vectors?
>
>tia,
>
>ssuukk
>
>--------------------------------
>
>alias real frReal;
>
>struct frVector
>{
>	frReal x; frReal y; frReal z;
>
>	void set(frReal xx, frReal yy, frReal zz) {
>		x=xx; y=yy; z=zz;
>	 }
>
>	void set(frVector vv) {
>		x=vv.x; y=vv.y; z=vv.z;
>	 }

frVector opNeg() {
frVector rtn;

rtn.x = -x;
rtn.y = -y;
rtn.z = -z;

return rtn;
}

>}
>


December 17, 2003
And if we had constructors for structs, it would be quite a bit more elegant.

frVector opNeg() { return frVector(-x,-y,-z); }

But we don't.   :(

Sean

"Patrick Down" <Patrick_member@pathlink.com> wrote in message news:brpva6$2nd2$1@digitaldaemon.com...
>
> This is what you need.
>
> In article <brppgb$2ei0$1@digitaldaemon.com>, ssuukk says...
> >
> >Sometime ago someone suggested that to make a vector that is located on stack instead of on heap one has to make it struct instead of class. I am trying it just now. The question is though, how to use operator overloading in such cases? Normally if it was a class I would new a return value, but here... hmmm... Can anyone write me an example of how to make add operator for two STRUCT vectors?
> >
> >tia,
> >
> >ssuukk
> >
> >--------------------------------
> >
> >alias real frReal;
> >
> >struct frVector
> >{
> > frReal x; frReal y; frReal z;
> >
> > void set(frReal xx, frReal yy, frReal zz) {
> > x=xx; y=yy; z=zz;
> > }
> >
> > void set(frVector vv) {
> > x=vv.x; y=vv.y; z=vv.z;
> > }
>
> frVector opNeg() {
> frVector rtn;
>
> rtn.x = -x;
> rtn.y = -y;
> rtn.z = -z;
>
> return rtn;
> }
>
> >}
> >
>
>


December 17, 2003
In article <brq7mn$1t2$1@digitaldaemon.com>, Sean L. Palmer says...
>
>And if we had constructors for structs, it would be quite a bit more elegant.
>
>frVector opNeg() { return frVector(-x,-y,-z); }
>
>But we don't.   :(
>
>Sean

Yeah, that would be nice.  I've started doing this
but it's not as nice.

struct Vector
{
static Vector make(float x, float y, float z)
{
Vector rtn;

rtn.x = x;
rtn.y = y;
rtn.z = z;

return rtn;
}

Vector opNeg()
{
return Vector.make(-x,-y,-z);
}
}



struct Vector
{

}


>
>"Patrick Down" <Patrick_member@pathlink.com> wrote in message news:brpva6$2nd2$1@digitaldaemon.com...
>>
>> This is what you need.
>>
>> In article <brppgb$2ei0$1@digitaldaemon.com>, ssuukk says...
>> >
>> >Sometime ago someone suggested that to make a vector that is located on stack instead of on heap one has to make it struct instead of class. I am trying it just now. The question is though, how to use operator overloading in such cases? Normally if it was a class I would new a return value, but here... hmmm... Can anyone write me an example of how to make add operator for two STRUCT vectors?
>> >
>> >tia,
>> >
>> >ssuukk
>> >
>> >--------------------------------
>> >
>> >alias real frReal;
>> >
>> >struct frVector
>> >{
>> > frReal x; frReal y; frReal z;
>> >
>> > void set(frReal xx, frReal yy, frReal zz) {
>> > x=xx; y=yy; z=zz;
>> > }
>> >
>> > void set(frVector vv) {
>> > x=vv.x; y=vv.y; z=vv.z;
>> > }
>>
>> frVector opNeg() {
>> frVector rtn;
>>
>> rtn.x = -x;
>> rtn.y = -y;
>> rtn.z = -z;
>>
>> return rtn;
>> }
>>
>> >}
>> >
>>
>>
>
>


December 17, 2003
Smooth!

C

"Patrick Down" <Patrick_member@pathlink.com> wrote in message news:brqa50$5im$1@digitaldaemon.com...
> In article <brq7mn$1t2$1@digitaldaemon.com>, Sean L. Palmer says...
> >
> >And if we had constructors for structs, it would be quite a bit more elegant.
> >
> >frVector opNeg() { return frVector(-x,-y,-z); }
> >
> >But we don't.   :(
> >
> >Sean
>
> Yeah, that would be nice.  I've started doing this
> but it's not as nice.
>
> struct Vector
> {
> static Vector make(float x, float y, float z)
> {
> Vector rtn;
>
> rtn.x = x;
> rtn.y = y;
> rtn.z = z;
>
> return rtn;
> }
>
> Vector opNeg()
> {
> return Vector.make(-x,-y,-z);
> }
> }
>
>
>
> struct Vector
> {
>
> }
>
>
> >
> >"Patrick Down" <Patrick_member@pathlink.com> wrote in message news:brpva6$2nd2$1@digitaldaemon.com...
> >>
> >> This is what you need.
> >>
> >> In article <brppgb$2ei0$1@digitaldaemon.com>, ssuukk says...
> >> >
> >> >Sometime ago someone suggested that to make a vector that is located
on
> >> >stack instead of on heap one has to make it struct instead of class. I am trying it just now. The question is though, how to use operator overloading in such cases? Normally if it was a class I would new a return value, but here... hmmm... Can anyone write me an example of
how
> >> >to make add operator for two STRUCT vectors?
> >> >
> >> >tia,
> >> >
> >> >ssuukk
> >> >
> >> >--------------------------------
> >> >
> >> >alias real frReal;
> >> >
> >> >struct frVector
> >> >{
> >> > frReal x; frReal y; frReal z;
> >> >
> >> > void set(frReal xx, frReal yy, frReal zz) {
> >> > x=xx; y=yy; z=zz;
> >> > }
> >> >
> >> > void set(frVector vv) {
> >> > x=vv.x; y=vv.y; z=vv.z;
> >> > }
> >>
> >> frVector opNeg() {
> >> frVector rtn;
> >>
> >> rtn.x = -x;
> >> rtn.y = -y;
> >> rtn.z = -z;
> >>
> >> return rtn;
> >> }
> >>
> >> >}
> >> >
> >>
> >>
> >
> >
>
>


December 18, 2003
ssuukk wrote:

> Sometime ago someone suggested that to make a vector that is located on stack instead of on heap one has to make it struct instead of class. I am trying it just now. The question is though, how to use operator overloading in such cases? Normally if it was a class I would new a return value, but here... hmmm... Can anyone write me an example of how to make add operator for two STRUCT vectors?
>
> tia,
>
> ssuukk
>
You should check out the maths.d class in dig.

-Anderson

> --------------------------------
>
> alias real frReal;
>
> struct frVector
> {
>     frReal x; frReal y; frReal z;
>         void set(frReal xx, frReal yy, frReal zz) {
>         x=xx; y=yy; z=zz;
>      }
>         void set(frVector vv) {
>         x=vv.x; y=vv.y; z=vv.z;
>      }
>         frVector opNeg() {
>         ????????
>     }
> }
>

December 18, 2003
> Yeah, that would be nice.  I've started doing this
> but it's not as nice.
> 
> struct Vector
> {
> static Vector make(float x, float y, float z)
> {
> Vector rtn;
> 
> rtn.x = x;
> rtn.y = y;
> rtn.z = z;
> 
> return rtn;
> }
> 
> Vector opNeg()
> {
> return Vector.make(-x,-y,-z);
> }
> }
> 
They say: "Best solutions are simple solutions". Brilliant! :-)

December 18, 2003
I made frVector struct with static make function as quasi-constructor. Strangely for some time in the day the compiler didn't complain, but now (in the second half of the day) it does:

	// niby-konstruktor
	static frVector make(frReal xx, frReal yy, frReal zz){
		frVector result;
		result.x=xx; result.y=yy; result.z=zz;
		return result;
	}

	frVector opMul(frVector vv){
		return frVector.make(vv.x*x,vv.y*y,vv.z*z);
	}

gives:

"undefined identifier module frVector.make" error. I don't get it. Why did it work before?