使用C ++ NULL vs 0 [英] Usage of C++ NULL vs 0

查看:65
本文介绍了使用C ++ NULL vs 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


作为C ++开发人员,我们应该使用它来指定指针,

NULL或0.


typedef DummyC DummyClass *; //在某个头文件中。


DummyC obj = NULL;


if(obj == NULL){

//它给人的印象是obj是一个指针

}





DummyC obj = 0;


如果(obj == 0){

//它给人的印象是obj是一个整数,这不是真的。

}


我理解NULL是一个宏并定义为0.但是为了可读性

目的应该是

我们使用NULL作为poiner。并且0表示整数。


此外,因为系统提供NULL定义为某个值(今天它是

0)并且如果

明天定义的值会发生变化,那么我们的代码仍然是

便携式

如果我们使用NULL。所以我认为不使用硬编号值0,我们

应该

使用NULL作为指针。它增加了可读性和

便携性。有什么建议吗?


问候,

-mukti

解决方案

muktipada写道:


您好,


作为C ++开发人员,我们应该使用它来指定指针,

NULL或0.


typedef DummyC DummyClass *; //在某个头文件中。



我相信你的意思


typedef DummyClass * DummyC;


>

DummyC obj = NULL;


if(obj == NULL){

//它给出obj是指针的印象

}





DummyC obj = 0;


if(obj == 0){

//它给人的印象是obj是一个整数,这不是真的。

}


我理解NULL是一个宏并定义为0.但是为了可读性

目的应该是

我们使用NULL作为poiner。 0表示整数。



使用''NULL''因为当''nullptr''成为语言的一部分时,它会更容易做到b $ b搜索和替换。


>

此外,由于系统提供NULL定义为某个值(今天它是

0)如果

明天定义的值发生变化,那么我们的代码仍将是

便携式

如果我们使用空值。所以我认为不使用硬编号值0,我们

应该

使用NULL作为指针。它增加了可读性和

便携性。有什么建议吗?



不要让你的大脑紧张系统提供NULL定义

的事情。标准说NULL是一个空指针常量

表达式。现在它是一个评估为0的表达式(它可以是b $ b,例如(123-123))。无论系统提供是什么不能不同

,真的。


V

-

请删除资金''A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问


7月25日,8:02 * am,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:


muktipada写道:
< blockquote class =post_quotes>
Hello,


作为C ++开发人员,我们应该使用它来指定指针,

NULL或0.


typedef DummyC DummyClass *; //在某个头文件中。



我相信你的意思


* * * typedef DummyClass * DummyC;



DummyC obj = NULL;


if(obj == NULL){

* //它给人的印象是obj是指针

}


OR


DummyC obj = 0;


if(obj == 0){

//它给人的印象是obj是一个整数,这是不正确。

}


我理解NULL是一个宏并定义为0.但是为了可读性

目的应该是

我们对poiner使用NULL。 0表示整数。



使用''NULL''因为当''nullptr''成为该语言的一部分时,它会更容易做到b $ b搜索和替换。


此外,由于系统提供NULL定义为某个值(今天它是

0)并且如果

明天定义的值会发生变化,那么我们的代码仍然是

便携式

如果我们使用NULL。所以我认为不使用硬编号值0,我们

应该

使用NULL作为指针。它增加了可读性和

便携性。有什么建议吗?



不要让你的大脑紧张系统提供NULL定义

的事情。 *标准说NULL是一个空指针常量

表达式。 *现在它是一个评估为0的表达式(它可以是b $ b,例如(123-123))。 *无论系统提供是什么不能不同

,真的。


V

-

请删除资金''A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问



是不是在C中,NULL有一个稍微不同的meaining -1?


如果你的代码仍在使用C组件,那可能会导致问题


puzzlecracker< ir ********* @ gmail.comwrote in news:f2184114-7967-4215-
84 *************** @ h17g2000prg.googlegroups.com :< br>


>我没有回复最热门的回复,请不要问



是不是在C中,NULL有一个稍微不同的meaining -1?


如果你的代码仍在使用C组件,那可能会导致问题


我不知道最新的标准是什么,但它至少曾经是真的

在C NULL中是((void *)0)至少在某些系统上。这是非法的
$ c $ b C ++。


joe


Hello,

As a C++ developer which one we should use for pointer assignment,
NULL or 0.

typedef DummyC DummyClass*; // in some header file.

DummyC obj = NULL;

if (obj == NULL) {
// It gives an impression that obj is a pointer
}

OR

DummyC obj = 0;

if (obj == 0) {
// it gives an impression that obj is an integer, which is not true.
}

I understand NULL is a macro and defined to 0. But for readibility
purpose should
we use NULL for poiner. and 0 for integer.

Also since system provide NULL to be defined to some value(today it is
0) and if
tomorrow the defined value changes, then still our code will be
portable
if we use NULL. So I think instead of using hard coaded value 0, we
should
use NULL for pointer. It increases both for readbility and
portability. Any suggestion?

Regards,
-mukti

解决方案

muktipada wrote:

Hello,

As a C++ developer which one we should use for pointer assignment,
NULL or 0.

typedef DummyC DummyClass*; // in some header file.

I believe you meant

typedef DummyClass * DummyC;

>
DummyC obj = NULL;

if (obj == NULL) {
// It gives an impression that obj is a pointer
}

OR

DummyC obj = 0;

if (obj == 0) {
// it gives an impression that obj is an integer, which is not true.
}

I understand NULL is a macro and defined to 0. But for readibility
purpose should
we use NULL for poiner. and 0 for integer.

Use ''NULL'' because when ''nullptr'' becomes part of the language, it would
be much easier to do a search-and-replace.

>
Also since system provide NULL to be defined to some value(today it is
0) and if
tomorrow the defined value changes, then still our code will be
portable
if we use NULL. So I think instead of using hard coaded value 0, we
should
use NULL for pointer. It increases both for readbility and
portability. Any suggestion?

Don''t strain your brain about the "system provide NULL to be defined"
thing. The Standard says that NULL is a null pointer constant
expression. Right now it is an expression that evaluates to 0 (it can
be, e.g. (123-123) ). Whatever the "system provide" cannot be different
from that, really.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Jul 25, 8:02*am, Victor Bazarov <v.Abaza...@comAcast.netwrote:

muktipada wrote:

Hello,

As a C++ developer which one we should use for pointer assignment,
NULL or 0.

typedef DummyC DummyClass*; // in some header file.


I believe you meant

* * *typedef DummyClass * DummyC;


DummyC obj = NULL;

if (obj == NULL) {
*// It gives an impression that obj is a pointer
}

OR

DummyC obj = 0;

if (obj == 0) {
// it gives an impression that obj is an integer, which is not true.
}

I understand NULL is a macro and defined to 0. But for readibility
purpose should
we use NULL for poiner. and 0 for integer.


Use ''NULL'' because when ''nullptr'' becomes part of the language, it would
be much easier to do a search-and-replace.

Also since system provide NULL to be defined to some value(today it is
0) and if
tomorrow the defined value changes, then still our code will be
portable
if we use NULL. So I think instead of using hard coaded value 0, we
should
use NULL for pointer. It increases both for readbility and
portability. Any suggestion?


Don''t strain your brain about the "system provide NULL to be defined"
thing. *The Standard says that NULL is a null pointer constant
expression. *Right now it is an expression that evaluates to 0 (it can
be, e.g. (123-123) ). *Whatever the "system provide" cannot be different
from that, really.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask

Isn''t true that in C, NULL has a slightly different meaining -1?

If you code is still working with C components, that might cause issues


puzzlecracker <ir*********@gmail.comwrote in news:f2184114-7967-4215-
84***************@h17g2000prg.googlegroups.com:

>I do not respond to top-posted replies, please don''t ask


Isn''t true that in C, NULL has a slightly different meaining -1?

If you code is still working with C components, that might cause issues

I don''t know what the latest standard says, but it at least used to be true
that in C NULL was ((void *)0) At least on some systems. This is illegal
in C++.

joe


这篇关于使用C ++ NULL vs 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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