Operator overloading in C ++

Suppose I have 2 class objects and it has one int data item. I want to add this integer data to another object and store the output in the first obj data object. I can overload the + operator and use the instruction as shown below

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

if i need to add as below

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

for above also i can overload the statement +.

but if I have 10+Xand I want to add 10 to the data item X, how can I do this?

+3
source share
6 answers

Similar:

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

( operator+usually should not be a member.)

operator+, overload +=. + + =. ( ), - :

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() {}
};

, : :

class MyClass : public ArithmeticOperators<MyClass>,
                MixedArithmeticOperators<MyClass, int>
+3

:

// 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;
}

, friend.


, / , , operator+, ++ - FAQ .
+2

+ - . operator + operator + ( 10 ).

+1

, , :

sample operator+(int leftOperand, const sample & rightOperand)
{
   //...
}
0

,

YourClass operator+(const YourClass &a, const YourClass&b) {

    // do the math here

}

, YourClass. YourClass, int.

So you have one operator+, and for all the rest intyou just create another constructor.

0
source

Although you can do this with the global + operator, I would advise against this.

Use only operator overloading for data types for which operators are immediately cleared, for example:

  • complex rooms
  • lines (+, - ok, but * probably doesn't make sense here)

The risk of operator overloading is that the compiler may perform unwanted conversions, especially if you did not make an explicit constructor with one argument.

0
source

All Articles