On May 24, 6:48 am, Neelesh <neelesh.bo...@gmail.com> wrote:
On May 24, 8:26 am, Ash <ashiru...@googlemail.com> wrote:
Why is this ok:
bool operator==(Foo* a, Foo& b) { return true; }
and if I have:
Foo* f1; Foo* f2;
then
f1 == *f2
is valid and calls the above function.
but the following (to try to override equality when called using
two raw pointers: f1 == f2):
bool operator==(const Foo* a, const Foo* b) { return true; }
doesn't compile?
(g++ tells me "test2.cpp:9: error: ?bool operator==(const Foo*,
const Foo*)? must have an argument of class or enumerated type")
Is it not possible to override the comparison of two pointers (f1
== f2) or have I simply not got the exact signature right?
g++ is telling exactly what the issue is: operator overloading is
allowed only when atleast one operand is a user defined type
(either a class or an enumeration type). A "pointer" is not a user
defined type (even if it is a pointer to a class type) and hence
it is not possible to overload any operator that takes two
pointers, eg. operator== or operator= or operator- etc.
I suspected as much, but in which case I don't udnerstand why the
first case:
bool operator==(Foo* a, Foo& b) { return true; }
is ok? Where's the logic of the C++ standards body in allowing this,
which is an operation on a pointer and not when both operands are?
already defined by the language. You cannot add another one.
name.