+ -
当前位置:首页 → 问答吧 → google c++ style与qt Coding Rules关于Null Pointers的分歧

google c++ style与qt Coding Rules关于Null Pointers的分歧

时间:2011-12-23

来源:互联网

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) 这种形式

分歧有没有,纠结有没有

作者: zmlovelx   发布时间: 2011-12-23

if(p)飘过

作者: sryan   发布时间: 2011-12-23

个人比较赞同google的规则

作者: akirya   发布时间: 2011-12-23

学习了,以前没见过。

作者: dahuaixiaohuai   发布时间: 2011-12-23

顶google, 0和0.0真木有用过.

作者: qq120848369   发布时间: 2011-12-23

支持google

作者: wodeprogrammer   发布时间: 2011-12-23

C++11 introduces a new keyword to serve as a distinguished null pointer constant: nullptr.

作者: yisikaipu   发布时间: 2011-12-23