Aquarius_Girl
Aquarius_Girl

Reputation: 22916

Different arguments in declaration and definition of the overloaded operators

Declaration inside the class:
Integer operator+ (const Integer& right);

Definition outside the class:

Integer operator+ (const Integer& left, const Integer& right)
{
    return left ;
}

What sense does it make for the compiler to enforce different number arguments in declaration and definition?

Upvotes: 0

Views: 166

Answers (3)

Hiren
Hiren

Reputation: 341

The first one is binary operator overloaded as member function and the second is binary operator overloaded as non-member function.

When an operator is defined as a member, the number of explicit parameters is reduced by one, as the calling object is implicitly supplied as an operand. Thus, binary operators take one explicit parameter and unary operators none. In the case of binary operators, the left hand operand is the calling object, and no type coercion will be done upon it.

This is in contrast to non-member operators, where the left hand operand may be coerced.

Upvotes: 4

Xeo
Xeo

Reputation: 131799

You declared two different operator+ there. The correct out-of-class definition would be this:

Integer Integer::operator+(const Integer& right)
{
    return *this;
}

Upvotes: 4

Naveen
Naveen

Reputation: 73443

Those are treated as two different methods i.e. it is treated as method overloading.

Upvotes: 2

Related Questions