C ++ 98 to check for null pointer

C ++ 11 introduced a keyword nullptrthat I don't have.

I assume that there is a macro NULLfrom C that I read about using in C ++ from some material here and here , but I'm still not sure if the correct way to check for a null pointer is in this older C ++ standard.

I essentially want to be able to write this code for my test case using the Boost test:

aWrapperDataStructure x;
BOOST_CHECK_NE(x.get_ptr(), static_cast<decltype(x.get_ptr())>(nullptr));

But maybe, as suggested in the Tutorials Point, something like this is more suitable given the limitations:

BOOST_CHECK(x.get_ptr()); //true when not NULL

Something about it threw me away, so I wonder what is the best practice here. If he is somewhere on the Internet, or on SO, he was buried for a long time, and I can not find him. Thank!

+4
source share
2 answers

I don’t feel right when answering my own question here because I’m definitely not ready, but I read this answer here (it is still labeled C ++, despite the newer standards) from James Kanze:

If you use g++, you really need to use NULL.

which is definitely enough for me. I found these expressions to check for a null pointer:

(p != NULL) //requires #include <stddef.h>

(p != 0)

(p)

3, , , bool (). != == bool, , NULL .

, g++, (p != NULL) ++. ( , , -.)

0

g++ ++ 11 .

nullptr "", .

pre-++ 11, NULL.

0

Source: https://habr.com/ru/post/1680802/


All Articles