In C++ I have commonly seen code that look something like this:
Object * p = init();
if (!p) {
printf("Fail");
}
else {
printf("Success");
}
As far as I can tell, it's checking to see if 'p' has been initialized correctly. We can achieve a similar result without the ! operator with the following if statements:
Object * p = init();
if (p) {
printf("Success");
}
else {
printf("Fail");
}
I've never actually read anywhere why this is done. I just know that it works. I know that conditionals in C++ can contain bools or integers where 0 is false and everything else is true, but how does it work with objects? I'm guessing maybe it has something to do with null pointers, but I've never seen it explicitly mentioned anywhere before.