Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Using ’0’ instead of ’NULL’
Niak74 last edited by
Hi everyone !
I'd like to know why Qt intend to uses 0 instead of NULL (null pointer).
Seen in Qt Documentation, example : QWidget::QWidget ( QWidget * parent = 0, Qt::WindowFlags f = 0 ) (http://doc.qt.nokia.com/4.6/qwidget.html).
Does Qt headers define NULL as 0 ?
Some code/dev rules are imposed in my company. So, can I use NULL instead of 0 (ex : QWidget::QWidget ( QWidget * parent = NULL, Qt::WindowFlags f = NULL ) ) or is it risky?
vsorokin last edited by
bq. Bjarne Stroustrup: In C++, the definition of NULL is 0
"Bjarne Stroustrup's C++ Style and Technique FAQ":http://www2.research.att.com/~bs/bs_faq2.html#null
yinyunqiao last edited by
0 is always 0, it can never be redefined as anything else.
ogoffart last edited by
This is just a matter of coding style.
If you whish to use NULL in your code, go for it.
But Qt code uses 0 because it is more c++ like.
Niak74 last edited by
Got it, thanks for your answers !
Have a nice day !
kernco last edited by
In C, NULL is preferred because depending on the system, it might not be defined as 0 (but I've never seen an example of this). In C++, NULL is defined as 0 in the standard, so both are safely interchangeable. Some people still like using NULL because in their opinion it makes the code clearer. As previous posters have said, it's up to the coder.
In most circumstances NULL is more obvious than 0, but it's only a convention.
(provided that you use 0 only for built-in value types) You can take NULL as an abbreviation for NULL pointer and easier skip the 0 literals, which are mostly trivial.
Reliance last edited by
Defining NULL as anything other than zero is an error. By using NULL, you are opening yourself up to the (unlikely) bug that somebody came along and munged the NULL manifest. But it's a lot harder for them to break 0.
maciej last edited by
"good article":http://mina86.com/2010/10/24/0-is-ambiguous/ about 0/NULL (and nullptr) in C/C++
Franzk last edited by
There are a lot of companies banning the use of defines and macros just because of the possible risk of breaking a lot of stuff by changing just one define/macro. Since NULL is no more than 0 and not (void*)0 as it sometimes is in C, and it therefore will not generate compiler errors when used inappropriately, I see little value in typing it out.
For your clarification: I'm aware that
@ #define NULL 0 @
...and surely I am a define-hater. I used 0 instead of NULL for more than a decade after reading Stroustrup. Today I feel better with NULL written out, maybe tomorrow this will change again. :)
[quote author="maciek" date="1291899880"]"good article":http://mina86.com/2010/10/24/0-is-ambiguous/ about 0/NULL (and nullptr) in C/C++[/quote] Thanks for the tipp, was interesting to read.