NULL宏与0作为空指针? [英] NULL macro vs. 0 as null pointer?

查看:59
本文介绍了NULL宏与0作为空指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。当在C ++中引用空指针常量时,是否有任何理由更喜欢在名为NULL的宏上使用0来定义

为0?


谢谢!


Ken

解决方案

" Ken" < KK **** @ yahoo.com>在消息中写道

大家好。当在C ++中引用空指针常量时,是否有任何理由更喜欢在名为NULL的宏上使用0来定义为0?




没有理由。 NULL更常见,而0看起来像是一个

整数,但人们总是使用两种方式。


我认为在C NULL中定义为0。在C ++中它是(void *)0。




" Ken" < KK **** @ yahoo.com>在消息中写道

news:51 ************************** @ posting.google.c om ...

大家好。当在C ++中引用空指针常量时,是否有任何理由更喜欢在名为NULL的宏上使用0来定义为0?




是的。值为0.为什么我们需要抽象呢?有时

抽象帮助,但在这种情况下,我不知道什么比0更清楚。

让我们说你做了这个

#define ONE 1


然后在你的代码中你做了这个


int x = ONE;


这对你有什么帮助吗?如果有一天ONE

的价值发生变化会怎样?通常你只想要这样一个宏,如果某些值的价值是b
而不是数字。我认为改变ONE的

值是一个非常糟糕的主意,它肯定不会比1更具可读性。


Siemel Naran写道:


" Ken" < KK **** @ yahoo.com>在消息中写道

大家好。当在C ++中引用空指针常量时,是否有任何理由更喜欢在名为NULL的宏上使用0来定义
为0?



我认为在C NULL中定义为0.在C ++中它是(void *)0。




其他方式。


Hi all. When referring to a null pointer constant in C++, is there
any reason to prefer using 0 over a macro called NULL that is defined
to be 0?

Thanks!

Ken

解决方案

"Ken" <kk****@yahoo.com> wrote in message

Hi all. When referring to a null pointer constant in C++, is there
any reason to prefer using 0 over a macro called NULL that is defined
to be 0?



There''s no reason. NULL is more commonplace whereas 0 looks like an
integer, but people use both ways all the time.

I think in C NULL is defined as 0. In C++ it is (void*)0.



"Ken" <kk****@yahoo.com> wrote in message
news:51**************************@posting.google.c om...

Hi all. When referring to a null pointer constant in C++, is there
any reason to prefer using 0 over a macro called NULL that is defined
to be 0?



Yes. The value is 0. Why do we need an abstraction of that? Sometimes
abstractions help, but in this case I don''t see what''s more clear than 0.
Let''s say you did this
#define ONE 1

Then in your code you did this

int x = ONE;

Does this help you in any way? What would happen if the value of ONE
changed someday? Normally you''d only want such a macro if the value of some
"magic number" actually were going to change in the future, or in some other
environment or platform, or if the name was somehow much more meaningful
than the number. I think it would be a really really bad idea to change the
value of ONE, ever, and it certainly isn''t any more readable than "1".


Siemel Naran wrote:


"Ken" <kk****@yahoo.com> wrote in message

Hi all. When referring to a null pointer constant in C++, is there
any reason to prefer using 0 over a macro called NULL that is defined
to be 0?


I think in C NULL is defined as 0. In C++ it is (void*)0.



Other way around.


这篇关于NULL宏与0作为空指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆