operator overloading

This is a discussion on operator overloading within the C++ Programming forums, part of the General Programming Boards category; well,if i want to add two objects of a same class e.g. fraction1+fraction2 and have developed the function for +.then ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    39

    Cool operator overloading

    well,if i want to add two objects of a same class e.g. fraction1+fraction2 and have developed the function for +.then should i simply do this
    fraction1+fraction2;
    which should call the overloaded operator function?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Yup. Exactly how it gets done depends on how you implement it. Often the object on the left hand side of the operator is implemented as the calling object and the object on the right hand side is the parameter passed to the operator.

    Now consider what you need to do if the you have an instance of type int or type double on one side or the other of the addition operator? The version you implemented only covers the case where both the lhs and rhs objects are of the user defined class. Let the fun begin.

Popular pages Recent additions subscribe to a feed

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