c ++,cpplint问题中if(pointer)与if(pointer!= NULL)之间的区别 [英] difference between if(pointer) vs if(pointer != NULL) in c++, cpplint issue

查看:118
本文介绍了c ++,cpplint问题中if(pointer)与if(pointer!= NULL)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了此帖子可以使用(指针)而不是if(指针!= NULL)?和网上的其他帖子。

I already checked this post Can I use if (pointer) instead of if (pointer != NULL)? and some other posts on net.

但它并未说明两个语句之间的任何区别。

But it is not stating any difference between two statements.

问题::当我在cpp代码上运行cpplint.py时,发现了检查指针是否为NULL的问题。
我更喜欢使用简单的

Problem: As I run cpplint.py on my cpp code, I found issues where I check pointers for NULL. I preferred to check using simple

if(pointer)         //statement1

,但 cpplint 说,您应该像

if(pointer != NULL)        //statement2

所以我只是想知道, statement2 相对于 statement1 有什么好处吗? statement1 在某些情况下可能会造成问题吗?

So I just want to know , Are there any benefits of statement2 over statement1 ? Are there some scenarios in which statement1 may create problem ?


工作方式:据我所知,这两种陈述在工作上均没有差异。

Working: As per my knowledge there is no difference in working of both statements. Its just a change of coding style.

我更喜欢像 statement1 这样使用,因为

I prefer to use like statement1, because


  • 简单易读

  • 没有因平等而误失的思绪( = == )进行比较

  • Its Simple, Readable
  • No Tension of missing (=) by mistake over equality(==) in a comparison

但是cpplint提出了这个问题,那么也许我错过了一些好处。

But cpplint is raising this as issue, then there might be some benefit that I missed.

注意: Java也不支持 statement1

Note: Java also doesn't support statement1.

推荐答案

否,如果 pointer 是实际上,指针类型没有区别,因此这里的所有内容都是编码风格的问题。反过来,编码风格取决于不同社区的习惯,因此不会有一般性建议。

No, if pointer is really a pointer type there is no difference, so everything here is a question of coding style. Coding style in turn depends on habits in different communities so there can't be a general recommendation.

我个人更喜欢第一个,因为它更短,更切合实际,避免使用伪宏 NULL

I personally prefer the first because it is shorter and more to the point and avoids the use of the bogus macro NULL.

在C中 NULL 可以是非常不同的东西(整数或指针),在C ++中,如今甚至不建议使用它。您至少应该在那里使用 nullptr

In C NULL can be very different things (integer or pointer) and in C++ its use is even deprecated nowadays. You should at least use nullptr, there.

这篇关于c ++,cpplint问题中if(pointer)与if(pointer!= NULL)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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