google c++ style与qt Coding Rules关于Null Pointers的分歧
google c++ style
0 and NULL
link ▽
Use 0 for integers, 0.0 for reals, NULL for pointers, and '\0' for chars.
Use 0 for integers and 0.0 for reals. This is not controversial.
For pointers (address values), there is a choice between 0 and NULL. Bjarne Stroustrup prefers an unadorned 0. We prefer NULL because it looks like a pointer. In fact, some C++ compilers, such as gcc 4.1.0, provide special definitions of NULL which enable them to give useful warnings, particularly in situations where sizeof(NULL) is not equal to sizeof(0).
Use '\0' for chars. This is the correct type and also makes code more readable.
Qt Creator Coding Rules
Null Pointers
Using a plain zero (0) for null pointer constants is always correct and least effort to type.
void *p = 0;
-NOT-
void *p = NULL;
-NOT-
void *p = '\0';
-NOT-
void *p = 42 - 7 * 6;
Note: As an exception, imported third party code as well as code interfacing the native APIs (src/support/os_*) can use NULL.
很多项目代码中指针直接用 p = 0; 来赋值 用if (p) 来判断
我个人一直用 p = NULL; if (p == NULL) 这种形式
分歧有没有,纠结有没有
[解决办法]
if(p)飘过
[解决办法]
个人比较赞同google的规则
[解决办法]
学习了,以前没见过。
[解决办法]
顶google, 0和0.0真木有用过.
[解决办法]
支持google
[解决办法]
C++11 introduces a new keyword to serve as a distinguished null pointer constant: nullptr.
[解决办法]
同意Google:
“We prefer NULL because it looks like a pointer. ”
[解决办法]
支持google的
[解决办法]
只有nullptr是完美和无歧义的。
其它,则,只要统一和习惯就好了,没有优劣之分。
[解决办法]
在 0 和 NULL 之间选择,我选 0
在 0 和 nullptr 之间选择,我选 nullptr
[解决办法]
在c++03以下,用0比NULL习惯,到c++11可以用nullptr了。
[解决办法]
赋值时一直用的 NULL
判断时
if (p) 和 if (p == NULL)
都用
[解决办法]
google和<<C/C++高质量编程>>中原则一样, 我也一直是这么用的, 当然比较时NULL在前