class Test
{
public:
operator Test * () { return NULL; };
};
int main()
{
Test test;
if (test == NULL)
printf("Wtf happened here?\n");
return 0;
}
How is it that this code compiles? How did Test get a comparison operator? Is there some implicit casting going around? What does that overloaded operator even mean (and do)?
The overloaded operator adds a conversion from
TesttoTest *. Since there is no comparision operator defined that takesTestandNULLas arguments, any conversion operators that exists are tried.operator Test *returns a type which is comparable withNULL, so it is used.