Using ’0’ instead of ’NULL’
-
wrote on 20 Jul 2010, 09:03 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?
Regards,
Niak74 -
wrote on 20 Jul 2010, 09:21 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
-
wrote on 20 Jul 2010, 10:16 last edited by
0 is always 0, it can never be redefined as anything else.
-
wrote on 20 Jul 2010, 10:56 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.
-
wrote on 20 Jul 2010, 12:15 last edited by
Got it, thanks for your answers !
Have a nice day !
Niak74
-
wrote on 21 Jul 2010, 20:15 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.
-
wrote on 9 Dec 2010, 11:37 last edited by
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. -
wrote on 9 Dec 2010, 12:54 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.
-
wrote on 9 Dec 2010, 13:04 last edited by
"good article":http://mina86.com/2010/10/24/0-is-ambiguous/ about 0/NULL (and nullptr) in C/C++
-
wrote on 9 Dec 2010, 13:30 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.
-
wrote on 9 Dec 2010, 14:57 last edited by
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. :)
-
wrote on 9 Dec 2010, 15:03 last edited by
[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.