Jump to content
  • Advertisement

Archived

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

jfclavette

On operator overloading

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
*grumble*

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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
______________________________________________________________

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!