0

I have found a lot of tutorials/snippets online, that suggest to use an overloading of this type:

NumeroFarlocco operator+(NumeroFarlocco n1, NumeroFarlocco n2) { ... }

But if I put this definition in my .h file:

NumeroFarlocco operator+(NumeroFarlocco n1, NumeroFarlocco n2);

I have this error: http://msdn.microsoft.com/it-it/library/1zy85x1e(v=vs.80).aspx

So I have figured out I have to define the overloading like this:

NumeroFarlocco operator+( NumeroFarlocco n2 );

but in the implementation I can't understand how to access the instance variables of the first element involved in the operation (the one that was n1 in my previous code), this->variable does not work, neither variable ...

1
  • 2
    Can you post your code, please? What you're describing appears to be valid. Commented Jan 31, 2013 at 16:25

5 Answers 5

2

There are two ways to overload a binary operator:

  • as a class member
  • as a free operator

Both are valid in different contexts:

struct A
{
    A operator+ (const A& other) const;
};

and

struct A
{};
operator+(const A& first, const A& second);

Note the use of const.

As a member, the two objects being added are *this and other. The first is the implicit current object.

As a free operator, the two objects are the ones passed as parameters.

Sign up to request clarification or add additional context in comments.

5 Comments

shouldn't the first operator (the class member) return a reference A& ?
@Shai no, you're thinking of +=. + returns a new object.
@LuchianGrigore "+ returns a new object" -> "+ should return a new object". The standard allows you to overload operator+ and give it the semantics of +=; common sense and respect for your collegues doesn't.
Yes, should return a new object. :)
I think it is worth adding some notes on the preferred method to use.
1
NumeroFarlocco operator+(NumeroFarlocco n1, NumeroFarlocco n2)

Should be an out-of-class definition, that is, a free function.

class NumeroFarlocco {
  // ...
};

NumeroFarlocco operator+(NumeroFarlocco n1, NumeroFarlocco n2) {
  // ...
}

If you using NumeroFarlocco operator+( NumeroFarlocco n2 ); it goes inside the class definition. Yes, the this pointer is how you access the left-hand-side value.

4 Comments

You don't really need the this pointer though (except for some cases with templates)
And if you need to access private variables to calculate your answer, you can declare the external function a friend of your class.
@JackAidley If you implement += with the right semantics, you don't need to access any private variables in +.
@JamesKanze: True, I was mentioning it for completeness more than anything else. Plus you may want to implement a separate operator+ for efficiency's sake under some circumstance.
0

Those are two different things,

The first method:

NumeroFarlocco operator+(NumeroFarlocco n1, NumeroFarlocco n2);

defines globally how to interprete the + operator.

That is it does means:

NumeroFarlocco n = operator+(n0,n2);

Where as the second one:

NumeroFarlocco operator+( NumeroFarlocco n2 );

defines how to interprete the addition of one to another.

that is:

NumeroFarlocco n = n0.operator+(n1)

Comments

0

I'd like to expand somewhat on Luchian Grigore's answer. Technically, as he says, there are two ways of defining binary operators. In practice, however, a certain number of conventions are usually followed; in particular, binary objects which return a new object (like operator+) are usually defined as non members, and those which modify the left operand (like operator+=) are usually defined as members. In addition, it is frequent to define the functions which return an object by using those which modify an object, e.g.:

MyType
operator+( MyType const& lhs, MyType const& rhs )
{
    MyType results( lhs );
    results += rhs;
    return results;
}

In fact, I don't normally even write the functions which return a value; I've written a base class template which defines them, and derive from that:

template <typename DerivedClass>
class MathematicOperators
{
    friend DerivedClass
    operator+( DerivedClass const& lhs, DerivedClass const& rhs )
    {
        DerivedClass results( lhs );
        results += rhs;
        return results;
    }
    //  And so on for all of the binary operators...
};

class MyClass : MathematicOperators<MyClass>
{
    //  ...
    MyClass& operator+=( MyClass const& other )
    {
        //  modify *this...
        return *this;
    }
    //  ...
};

(Note that the friend in MathematicOperators is not to be able to access private members of the derived class, but to be able to define the free function inline in the class template.)

I would strongly recommend following this procedure. It will result in a lot less code, and will guarantee that + and += have the expected relationship.

Comments

0

First of all you might want to read the following tutorial to learn some basic rules on how to overload operators.

Starting with your original code :

NumeroFarlocco operator+(NumeroFarlocco n1, NumeroFarlocco n2);

This is might not what you really want to declare. The way you declare operator+ is dependant on what you want to achieve.

Assuming from your question, you want to overload the binary+, so you would do it like this:

friend NumeroFarlocco operator+(NumeroFarlocco n1 , NumeroFarlocco n2 );

or alternatively as you declared above :

NumeroFarlocco operator+( NumeroFarlocco n2 );

The difference between these two declaration is that the first one is friend(free funcion) while the second one is a member function.

The first one(using friend function) means you don't call the function via object (it's like a global function), the second one means that you must call this function via object (as you normally do with member function).

Also, always remember that operator is a function.

NumeroFarlocco x, y, z; // suppose you have empty/default c'tor

x = y + z  is equivalent to x.operaotr=(y.operator+(z))

Hope it helps you, Syndicator!

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.