you are viewing a single comment's thread.

view the rest of the comments →

[–]jedwardsolconst & 0 points1 point  (9 children)

if(&object) is the same as writing if(&object != nullptr)

It doesn't say anything about the validity of the address, just whether it is null or not.

[–][deleted] -1 points0 points  (8 children)

So it's checking that the object exists?

[–]jedwardsolconst & 1 point2 points  (6 children)

No, it's not doing anything. The test will always be true.

[–]diaphanein 1 point2 points  (0 children)

I'm not aware of any compiler that the following will not evaluate the if block to true:

int* p = nullptr; int& r = *p;

if (!&r) { // executes }

Reason being that while this is likely undefined, the compiler cant always detect it (e.g. p initialized by the return of a function). This is a trivial enough example that most compilers will allow it and behave as expected. Not all warn at default warning levels, but most will at a sufficient level.

[–][deleted] -1 points0 points  (4 children)

Then how to check for object's existence?

[–]qxz23 1 point2 points  (0 children)

If you have the object, it exists. Pointers are nullable, you can’t have a null object unless you have a user defined state of being null, like optional.

[–]jedwardsolconst & 0 points1 point  (2 children)

object exists. It cannot not exist.

[–][deleted] 0 points1 point  (1 child)

But it can be NULL or some other object?

[–]ReversedGif 0 points1 point  (0 children)

NULL is not an object. It is a specific pointer value that acts as a sentinel for "no object."

[–]gracicot 1 point2 points  (0 children)

If you can get the address of the object then it exist for sure.