4

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?

1
  • You need a global function operator+ with two arguments. Commented Mar 14, 2011 at 10:43

6 Answers 6

3

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

Don't overload the operator + as a member function of the class. You can either define a global function operator + with two parameters or make operator + a friend of your class (In that case you should be having a parameterized constructor to convert 10 to an object of your class-type).

3 Comments

could you please explain why?
@zombie : Read this C++-FAQ on operator overloading : stackoverflow.com/questions/4421706/operator-overloading
Cause operator overloads as member functions always expect the left class to be your class (e.g. class + 10). While some operations might allow you to swap parameters if you calculate it by hand (addition, multiplikation) the compiler won't be able to do so.
0

Define a non-member stand-alone free function as:

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

Comments

0

Although you can do that using a global operator+, I would advise not to do it.

Only use operator overloading for data types for which the operators are immediately clear, e.g.:

  • complex numbers
  • strings (+,- ok, but * probably doesn't make much sense here)

The risk with overloaded operators is that the compiler may perform unwanted conversions, especially if you didn't make the single-argument constructor explicit.

3 Comments

What does global have to do with anything? A freestanding operator-function needn't be global
It is not any more global than a class method.
My suggestion was not to use operator overloading at all, unless for 'logical' cases (complex numbers, ...).
0

You should define a non-member friend function

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

    // do the math here

}

it should be friend to get to the private members of YourClass. Also you should create constructor for YourClass that takes int.

In this way you've got one operator+ and for every other then int you just create another constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.