Why is it called operator overloading?

Viewed 489

If the following class, Foo, is defined. It is said it overloads the unary ampersand (&) operator:

class Foo {
public:
   Foo* operator&() { return nullptr; }
};

I think in this case, (reglardless of the fact that you can get the address of such an object by means of std::addressof() and other idiomatic tricks) there is no way to access/choose the original unary ampersand operator that returns the address of the object called on, am I wrong?

By overloading however, I understand that there is a set of functions of which one will be selected at compile-time based on some criteria. But this thinking doesn't seem to match the scenario above.

Why is it then called overloading and not something else like redefining or replacing?

2 Answers

You can't redefine a function or operator in C++, you can add only new use to it, defining new set of arguments. That's why it called overloading instead of redefining.

When you overload operator as a member of class you

1) defined it with first argument supposed to be an instance of that class

2) gave it access to all members of that class.

There are still definitions of operator& with different arguments and you have a non-zero chance to create situation where use of operator would be ambigous.

Related