A question about operator overloading

This is a discussion on A question about operator overloading within the C++ Programming forums, part of the General Programming Boards category; I have a 3D vector class. I have overloaded operator *= such that it can take a scaler value on ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    4

    A question about operator overloading

    I have a 3D vector class. I have overloaded operator *= such that it can take a scaler value on the right hand side. That is working fine.

    Now I have a second loaded operator* which takes a Vector3D arg AND a scaler arg.

    So, it would work thus:

    Vector3D scaledVec = oldVec * 3.0;

    My question is, how would I make the operation commutative such that the following would also be valid:

    Vector 3D scaledVec = 3.0 * oldVec;

    Must I overload the function?

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Create a global overloaded version of the function, something like

    operator* ( double , Vector3D& )

    I'm in the middle of studying for a psychology test, so if I'm talking rubbish, ignore me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21