C++中为什么要尽量使用0而不是NULL

「已注销」 2011-12-14 10:25:47
今天看了 Bjarne Stroustrup 的百科,在忠告里面看到他说的一句话
3. 尽量使用0而不是NULL。
我在判断指针的时候习惯是写成NULL,记得好像是不同环境下NULL的值定义的不一样,写NULL比较有通用性。
e.g.

if(p == NULL)
{
//...
}

如果这里写成了

if(p == 0)
{
//...
}

那移植的时候是不是会存在问题,或者说,哪种写法更能符合ANSI C++的标准,因为我感觉遵循标准才是比较好的。
不知道我的理解对不对,还有不太清楚 Bjarne Stroustrup 的真正意思是什么?请各位指点:)
...全文
1716 49 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
49 条回复
切换为时间正序
请发表友善的回复…
发表回复
guozizheng001 2014-08-27
  • 打赏
  • 举报
回复
引用 32 楼 zhouquan2009 的回复:
[Quote=引用 30 楼 cxxer 的回复:] C/C++ code nullptr int aa(int ii) {} int aa(char * cc ) {} aa(0); //aa(ii) or aa(cc)? [/Quote] 我试了一下您的代码,发现 aa(0) 调用了 aa(ii) 这个函数,我的理解是 0 是 int 和 int aa(int ii) {} 是最佳的匹配,所以不会报重载歧义的错误 我的环境是 DEV 4.9.9.2,Windows 7 32bit,这是我的代码

#include <iostream>
using namespace std;

int foo(int i) { cout << "foo(int i)" << endl; }

char *foo(const char *sz) { cout << "foo(const char *sz)" << endl; }

int main()
{   
    foo(0);
    system("pause");
    return 0;
}
其中 char *foo() 函数的形参带不带 const 结果是一样的。 不知道我的理解对不对 :)
那么用NULL呢?
verycpp 2014-01-21
  • 打赏
  • 举报
回复
引用 18 楼 w233052085 的回复:
用NULL可以清楚的让人明白 这个是指针,而非其他什么东西,如果是0的话就不好说了。 用什么 还是得看统一规范,不是为了对编译器更加友好,而是为了对人阅读更加友好。
支持,写代码就是给人看的嘛~
ymecho 2013-08-24
  • 打赏
  • 举报
回复
引用 46 楼 chejianchao2012 的回复:
[quote=引用 20 楼 jennyvenus 的回复:] 我不信他说的话,即使他是C++他老爸也不信。 这世界上有很多人是乱说的,比如定义指针时,那个*是挨着变量类型的后面,还是放在变量的前面。 高人们就说一定要放在变量类型的后面 人家是这么推荐的
int* a, b;
俺一直这么写
int *a, b;
呃,写成int* a有一定道理,int* 代表的是类型,整型指针,意思就是定义一个整型指针a,int *a表示理解不能,虽然你知道是整型指针,不过,该怎么解释呢?我觉得解释不通。[/quote] 按你这么说,后面的b也是指针了。。
chejianchao2012 2013-08-22
  • 打赏
  • 举报
回复
引用 20 楼 jennyvenus 的回复:
我不信他说的话,即使他是C++他老爸也不信。 这世界上有很多人是乱说的,比如定义指针时,那个*是挨着变量类型的后面,还是放在变量的前面。 高人们就说一定要放在变量类型的后面 人家是这么推荐的
int* a, b;
俺一直这么写
int *a, b;
呃,写成int* a有一定道理,int* 代表的是类型,整型指针,意思就是定义一个整型指针a,int *a表示理解不能,虽然你知道是整型指针,不过,该怎么解释呢?我觉得解释不通。
forjoy 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 jennyvenus 的回复:]

我不信他说的话,即使他是C++他老爸也不信。

这世界上有很多人是乱说的,比如定义指针时,那个*是挨着变量类型的后面,还是放在变量的前面。

高人们就说一定要放在变量类型的后面

人家是这么推荐的

C/C++ code
int* a, b;


俺一直这么写
C/C++ code
int *a, b;
[/Quote]

我还是喜欢把指针变量和普通变量拆开写,并在定义时将指针变量初始化:

char* p = NULL;
char a, b, c;
FrankHB1989 2011-12-19
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 jennyvenus 的回复:]

我不信他说的话,即使他是C++他老爸也不信。

这世界上有很多人是乱说的,比如定义指针时,那个*是挨着变量类型的后面,还是放在变量的前面。

高人们就说一定要放在变量类型的后面

人家是这么推荐的

C/C++ code
int* a, b;


俺一直这么写
C/C++ code
int *a, b;
[/Quote]
写两行就真那么累么。。。
虽然int *a, b, (*p)[10], (*fp)(int), f(void);啥的也没压力就是了╮(╯_╰)╭。
FrankHB1989 2011-12-19
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 gbb21 的回复:]

引用 10 楼 frankhb1989 的回复:

引用 8 楼 yisikaipu 的回复:

引用 6 楼 shenchenman 的回复:扯淡。。最好用NULL,跨平台通用,而且这也要看场合对指针而言都用NULL,这在华为编程规范里明确说明

如果NULL就定义为0的话,不存在跨平台问题,0才是最跨平台的,如果不定义成0的话,它也不该叫做NULL

写成NULL,无非是为了……
[/Quote]
你引错了。11L倒是有符合你指谪的烧饼的地方。
三连你懂的。
Ndk 2011-12-15
  • 打赏
  • 举报
回复
我是这样 定义 NULL 的

先 undef

然后
#define NULL ((void *)0)
yisikaipu 2011-12-15
  • 打赏
  • 举报
回复
敲错了个单词,defined而不是fefined

因为pdf拷贝下来有些字符不对,改了下不小心改错了
yisikaipu 2011-12-15
  • 打赏
  • 举报
回复
C99标准

7.17 Common definitions <stddef.h>
1 The following types and macros are fefined in the standard header <stddef.h>. Some are also defined in other headers, as noted in their respective subclauses.
...
3 The macros are
NULL
which expands to an implementation-defined null pointer constant; and ...
「已注销」 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 41 楼 supermegaboy 的回复:]

引用 31 楼 zhouquan2009 的回复:
首先感谢各位兄弟的热心解答,抱歉不能一一回复了 :(
看了各位兄弟的回复,我感觉我明确的一些东西:
1、NULL 并不是 C++ 的关键字,是一些类库中定义的一个宏,可能这个库是 Windows SDK,也可能是 C++ 为了兼容 C 而在 cstdio、cstddef 中定义的宏;
2、新的 C++ 标准中出现了关键字 nullptr……
[/Quote]

有什么指点
飞天御剑流 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 zhouquan2009 的回复:]
首先感谢各位兄弟的热心解答,抱歉不能一一回复了 :(
看了各位兄弟的回复,我感觉我明确的一些东西:
1、NULL 并不是 C++ 的关键字,是一些类库中定义的一个宏,可能这个库是 Windows SDK,也可能是 C++ 为了兼容 C 而在 cstdio、cstddef 中定义的宏;
2、新的 C++ 标准中出现了关键字 nullptr 用来给指针赋空值;
3、写程序要看具体的环境,可能不……
[/Quote]
你的C++真是白学了........
「已注销」 2011-12-15
  • 打赏
  • 举报
回复
真心感谢各位兄弟的耐心解答和指导,结贴啦~~
「已注销」 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 yisikaipu 的回复:]

C99标准

7.17 Common definitions <stddef.h>
1 The following types and macros are fefined in the standard header <stddef.h>. Some are also defined in other headers, as noted in their respective subcl……
[/Quote]

十分感谢 :)
「已注销」 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 frankhb1989 的回复:]

引用 4 楼 healer_kx 的回复:

C++中有NULL? NULL不是Windows SDK里面定义的嘛?

不过确实开始用nullptr了。。。

<cstddef>

不考虑旧代码就nullptr,其实自己也可以实现一个,不过套了命名空间的重载里面参数需要隐式转换的时候可能会有点问题……
其它情况用NULL。不过考虑int重载,确实是有产生误导的可能性。


……
[/Quote]

您好,我查看了下DEV 4.9.9.2 的 include 文件夹,在 cstddef 和 stddef.h 中都没有找到 NULL 的宏定义,不知道是不是我的方法不对 :)
cstddef

// -*- C++ -*- forwarding header.

// Copyright (C) 1997, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

//
// ISO C++ 14882: 18.1 Types
//

/** @file cstddef
* This is a Standard C++ Library file. You should @c #include this file
* in your programs, rather than any of the "*.h" implementation files.
*
* This is the C++ version of the Standard C Library header @c stddef.h,
* and its contents are (mostly) the same as that header, but are all
* contained in the namespace @c std.
*/

#ifndef _GLIBCXX_CSTDDEF
#define _GLIBCXX_CSTDDEF 1

#pragma GCC system_header

#include <stddef.h>

namespace std
{
using ::ptrdiff_t;
using ::size_t;
}

#endif


cstddef.h

/*
* This is just an RC_INVOKED guard for the real stddef.h
* fixincluded in gcc system dir. One day we will delete this file.
*/
#ifndef RC_INVOKED
#include_next<stddef.h>
#endif
「已注销」 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 cxxer 的回复:]

C/C++ code
nullptr

int aa(int ii) {}
int aa(char * cc ) {}

aa(0); //aa(ii) or aa(cc)?
[/Quote]

我试了一下您的代码,发现 aa(0) 调用了 aa(ii) 这个函数,我的理解是 0 是 int 和 int aa(int ii) {} 是最佳的匹配,所以不会报重载歧义的错误
我的环境是 DEV 4.9.9.2,Windows 7 32bit,这是我的代码

#include <iostream>
using namespace std;

int foo(int i) { cout << "foo(int i)" << endl; }

char *foo(const char *sz) { cout << "foo(const char *sz)" << endl; }

int main()
{
foo(0);
system("pause");
return 0;
}

其中 char *foo() 函数的形参带不带 const 结果是一样的。
不知道我的理解对不对 :)
「已注销」 2011-12-15
  • 打赏
  • 举报
回复
首先感谢各位兄弟的热心解答,抱歉不能一一回复了 :(
看了各位兄弟的回复,我感觉我明确的一些东西:
1、NULL 并不是 C++ 的关键字,是一些类库中定义的一个宏,可能这个库是 Windows SDK,也可能是 C++ 为了兼容 C 而在 cstdio、cstddef 中定义的宏;
2、新的 C++ 标准中出现了关键字 nullptr 用来给指针赋空值;
3、写程序要看具体的环境,可能不同的厂家、公司、开发团队不同的编码规范中会有不同的要求;
4、大师的某个推荐也不一定是最优的,需要在具体的上下文中理解,不可望文生义;
不知道自己的理解是不是还有偏差

最后,真的十分感谢各位兄弟,愿意和大家一起感受 C++ 的魅力和精彩 :)
zhujian888 2011-12-15
  • 打赏
  • 举报
回复
同意楼上观点
yusss 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 jennyvenus 的回复:]

我不信他说的话,即使他是C++他老爸也不信。

这世界上有很多人是乱说的,比如定义指针时,那个*是挨着变量类型的后面,还是放在变量的前面。

高人们就说一定要放在变量类型的后面

人家是这么推荐的

C/C++ code
int* a, b;


俺一直这么写
C/C++ code
int *a, b;
[/Quote]
绝对赞同!
独自听枫 2011-12-14
  • 打赏
  • 举报
回复
用NULL可以清楚的让人明白 这个是指针,而非其他什么东西,如果是0的话就不好说了。
用什么 还是得看统一规范,不是为了对编译器更加友好,而是为了对人阅读更加友好。
加载更多回复(29)

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧