Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator overloading, member and non-member function, which one has priority?

Say I have a Complex number class and operator+ is overloaded twice, both as a member function and as a global function, for example:

class Complex {
public:
    Complex operator+(const Complex& c);
};
Complex operator+(const Complex& a, const Complex& b);

And in the main function I will call the operator+ like follows:

Complex a, b;
Complex c = a + b;

Which operator+ function will be called? Thanks!

like image 986
Brian Avatar asked Feb 27 '14 23:02

Brian


People also ask

When should we overload an operator through a non member function?

There are two situations under which operator overloading must be done by functions that are not members of a specific class. These situations are: the class to which the member function "should" be added is not available for modification. This frequently occurs with classes that are in standard class libraries.

Is operator overloading a member function?

Operator overloading of member function When overloading an operator using a member function: The overloaded operator must be added as a member function of the left operand. The left operand becomes the implicit *this object. All other operands become function parameters.

What is difference between operator overloading using member function and friend function?

A member function can be declared in private, public or protected scope of the class but have scope accordingly. While overloading binary operator, friend function takes two arguments. While overloading binary operator, member function takes one arguments.

Which operator is required to be overloaded as a member function only?

Explaination. Overloaded assignment operator does the job similar to copy constructor and is required to be overloaded as member function of the class.


1 Answers

Members are not preferred over non-members in general, nor vice versa. C++'s overload resolution rules are applied to select one or the other.

A member function, for the purpose of overload resolution, is considered to have an implied object parameter (§13.3.1/2). So

Complex Complex::operator+(const Complex& c);

is treated as though it takes two arguments: the original const Complex& c, and another Complex& which refers to the object used to call the member function (in effect, *this).

Suppose we have two Complex variables:

Complex c1, c2;

Both c1 and c2 are non-const, so in order to call

c1.operator+(c2)

the parameter c, which is a const reference, has to bind to the non-const argument c2.

On the other hand, to call

operator+(c1, c2)

both parameters a and b, which are const references, have to bind to non-const objects, c1 and c2.

The member operator+ is better because const Complex&, Complex& is a better match for c1, c2 than const Complex&, const Complex& because it performs less qualification conversion. (§13.3.3.2/3)

If you change the declaration of the member operator+ to

Complex Complex::operator+(const Complex& c) const;

then the overload will become ambiguous, and compilation will fail.

like image 200
Brian Bi Avatar answered Oct 26 '22 20:10

Brian Bi