Public Group

#### Archived

This topic is now archived and is closed to further replies.

This topic is 5283 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Vector Vector::operator*(const int &k) {
return Vector(mX*k, mY*k, mZ*k);
}

.....

Vector vec;
vec = vec * 2; //OK
vec = 2 * vec; //WRONG

Is there a way to solvethis little problem ?

##### Share on other sites
Create a non member overload. It should go like this:
Vector operator*(int lhs, const Vector & rhs) {  return rhs * lhs;}

Oh, and while you're at it, make your member operator* overload const.

edit: formatting
edit: one of these days, I'm going to be able to tell my right from my left

[edited by - SiCrane on June 1, 2004 9:08:56 PM]

##### Share on other sites
Thanks a lot,

Yeah, the const appeared betweem the post and the answer

##### Share on other sites
*grumble*

I never understood why there wasn''t a commutative keyword so the compiler could do that for you

##### Share on other sites
Indeed.

And i''d kill a teammate who would make A*B differently from B*A anyway.

##### Share on other sites
quote:
Original post by xMcBaiNx
And i'd kill a teammate who would make A*B differently from B*A anyway.

time to kill matrices and quaternions, among many other things, which both give you different results depending on the order of multiplication... alas, i really liked them...

-me

[edited by - Palidine on June 1, 2004 9:24:33 PM]

##### Share on other sites
quote:
Original post by Zipster
*grumble*

I never understood why there wasn''t a commutative keyword so the compiler could do that for you

I believe something like that was proposed to the standards committee, but was rejected primarily because C++ was enough of a bloody mess that the cost of using a new keyword was judged more than the benefit. (My personal interpretation of events may not 100% match factual accounts.)

##### Share on other sites
quote:
Original post by Palidine
quote:
Original post by xMcBaiNx
And i''d kill a teammate who would make A*B differently from B*A anyway.

time to kill matrices and quaternions, among many other things, which both give you different results depending on the order of multiplication... alas, i really liked them...

-me

Yeah, but the ideal implementation imho would be to define both side, like nonmember operators. That way, you would still have control over situations like this.

Sidenote: Vector * Vector
Make it the dotproduct, the crossproduct, or none to avoid confusion ?

[edited by - Palidine on June 1, 2004 9:24:33 PM]

##### Share on other sites
quote:
Original post by xMcBaiNx

Sidenote: Vector * Vector
Make it the dotproduct, the crossproduct, or none to avoid confusion ?

IMO, none.

##### Share on other sites
quote:
Original post by SiCrane
quote:
Original post by xMcBaiNx

Sidenote: Vector * Vector
Make it the dotproduct, the crossproduct, or none to avoid confusion ?

IMO, none.

This leads me to something that''s been bugging me for a little while now. What do you do with formulas involving velocity squared? My best guess would be to convert the velocity to polar form, square the length, and convert the vector back to standard (x, y) form, but I really have no idea if that''s right.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633410
• Total Posts
3011723
• ### Who's Online (See full list)

There are no registered users currently online

×