Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator overloading in c++

suppose i have 2 objects of a class and it has one int data member. i want to add those integer data to other object and store the output in the first obj's data member.I can overload the + operator and use the statement like below

X+Y  //where X and Y are objects of one class.

if i have to add like below

X+10// here i want to add 10 to the data member of X.

for above also i can overload the operator +.

but if i have 10+X and i want to add 10 to the data member of X how could i do it?

like image 993
Vijay Avatar asked Oct 23 '25 20:10

Vijay


2 Answers

The same way:

MyClass operator+(MyClass const& lhs, MyClass const& rhs);
MyClass operator+(MyClass const& lhs, int rhs);
MyClass operator+(int lhs, MyClass const& rhs);

(operator+ should not normally be a member.)

If you overload operator+, you'll also want to overload +=. One frequent idiom involved implementing + in terms of +=. This can be more or less automated (if you have a lot of classes supporting operators) by defining something like:

template<typename DerivedType>
class ArithmeticOperators
{
  public:
    friend DerivedType operator+(
        DerivedType const&  lhs,
        DerivedType const&  rhs)
    {
        DerivedType         result(lhs);
        result += rhs;
        return result;
    }
    //  And so on for the other operators...
protected:
    ~ArithmeticOperators() {}
};

template<typename DerivedType, typename OtherType>
class MixedArithmeticOperators
{
  public:
    friend DerivedType operator+(
        DerivedType const&  lhs,
        OtherType const&    rhs)
    {
        DerivedType         result(lhs);
        result += rhs;
        return result;
    }
    friend DerivedType operator+(
        OtherType const&    lhs,
        DerivedType const&  rhs)
    {
        DerivedType         result(rhs);
        result += lsh;
        return result;
    }
    //  And so on: non-commutative operators only have the
    //  first.
protected:
    ~MixedArithmeticOperators() {}
};

, then deriving from whatever is needed: in your case:

class MyClass : public ArithmeticOperators<MyClass>,
                MixedArithmeticOperators<MyClass, int>
like image 108
James Kanze Avatar answered Oct 26 '25 09:10

James Kanze


You have to create an overloaded operator as a free function with the correct parameter order:

// This will match "int + YourClass" additions
YourClass operator+(int Left, const YourClass & Right)
{
    // If your addition operation is commutative, you can just call the other
    // version swapping the arguments, otherwise put here your addition logic
    return Right + Left;
}

If the operator needs to fiddle with the internals of your class you can make it friend.


As others pointed out, there are some best/common practices that you should follow if you implement operator+, I suggest you to have a look to the great C++-FAQ on operator overloading for more info about them.
like image 31
Matteo Italia Avatar answered Oct 26 '25 09:10

Matteo Italia