Mais um assunto bacana que foi discutido na lista CPPBRASIL.
O André fez algumas observações sobre o uso de NULL ou 0 e o Hugo complementou com informações bem interessantes. Essa informação é bem bacana para os novatos como eu que está tentando aprender essa complexa e poderosa linguagem que é o C++.
Segue abaixo o trecho da mensagen entre eles:
André comenta:
É um assunto meio polêmico. Realmente acho que o nullptr (ou algo semelhante
mas com outro nome) deveria ter sido considerado padrão C++ a muito tempo. A
maioria dos programadores deve utilizar o NULL.O que eu acho engraçado é que o próprio Stroustrup, junto com Ellis
(Annotated C++ Reference Manual), escreveu sobre o assunto, dizendo que é
recomendável evitar-se o NULL, pois não há 100% de certeza quando a sua
implementação em todos os casos, e que em alguns casos ele pode ser definido
como (void*)0. Alguém aí já teve algum problema desse tipo? De qualquer
forma, talvez seja precipitado considerar o uso do 0 como prática amadora
(sei que talvez não seja isso que você quis dizer exatamente). Alguém aí
utiliza 0 ao invés de NULL?
Hugo responde:
Sempre usei 0 e *nunca* tive problemas, acho C’ismo pensar que usar 0 ao invés
da macro NULL seja algo amador. Para não dizer que estou sozinho, o pessoal do
KDE[1] também usa 0 ao invés de NULL, bem, na verdade eles recomendam que você
use o que achar melhor, por que em C++ isso acaba sendo uma questão de
preferência pessoal e não técnica, eis a explicação deles:You may notice that null pointers are marked variously in one of three ways:
0, 0L and NULL. In C, NULL is defined as a null void pointer. However, in C++,
this is not possible due to stricter type checking. Therefore, modern C++
implementations define it to a “magic” null pointer constant which can be
assigned to any pointer. Older C++ implementations, OTOH, simply defined it to
0L or 0, which provides no additional type safety – one could assign it to an
integer variable, which is obviously wrong.In pointer context, the integer constant zero means “null pointer” -
irrespective of the actual binary representation of a null pointer. This means
that the choice between 0, 0L and NULL is a question of personal style and
getting used to something rather than a technical one – as far as the code in
KDE’s SVN goes you will see 0 used more commonly than NULL.Note, however, that if you want to pass a null pointer constant to a function
in a variable argument list, you *must* explicitly cast it to a pointer – the
compiler assumes integer context by default, which might or might not match
the binary representation of a pointer. Again, it does not matter whether you
cast 0, 0L or NULL, but the shorter representation is generally preferred.[1]
http://techbase.kde.org/Development/Tutorials/Common_Programming_Mistakes#NULL_pointer_issues