Hi guys
Please take a look at this example:
Code:
class X {
public:
// member prefix ++x
void operator++() { }
};
class Y { };
// non-member prefix ++y
void operator++(Y&) { }
int main() {
X x;
Y y;
// calls x.operator++()
++x;
// explicit call, like ++x
x.operator++();
// calls operator++(y)
++y;
// explicit call, like ++y
operator++(y);
}
Please note that when using the overloaded operator for an instance of X, we can call it using
In the case with the overloaded operator ++ for an instance of Y (it is overloaded using a non-member function), we can call it using
My question is: Is there any reasoning behind the fact that we cannot call the two operators using the same syntax? Or is that just the way it is?
Best,
Niles.