Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ Infixes

Options
  • 21-01-2002 10:56pm
    #1
    Registered Users Posts: 2,281 ✭✭✭


    i have googled about for the last few minutes, and i can't find anything on it.

    Can you make your own custom infixes in c++?

    I have a class Vector and class quaterion and i want to be able to type:

    Vector a,b,c;
    .....
    c = a + b;

    instead of

    c = a.add(b);


Comments

  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    yeah, I think what your looking for is operator overloading.
    Something like this:
    Vector Vector::operator+ ( const Vector& v1, const Vector& v2 ){
          Vector v;
    
          v.x = v1.x + v2.x ;
          v.y = v1.y + v2.y ;
          v.z = v1.z + v2.z ;
    
          return v;
    }
    


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    Thanks a million Jazz.

    BTW the freaky thing is that your code went into mine without any editing :/

    My class Vector has 3 public doubles called x,y,z.


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    Its that darn Burtan Meyer and his eiffel that has me calling the things infixes.

    !! pling pling.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma




  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    Great minds think alike, eh? :D


  • Advertisement
Advertisement