在C ++中检查Null指针的首选方法是什么? [英] What is the preferred way to check for a Null pointer in C++?

查看:123
本文介绍了在C ++中检查Null指针的首选方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选项A:

if (NULL == pSomethingColumn) // Yes, we use Yoda conditions
if (NULL != pSomethingColumn)

if (pSomethingColumn)
if (!pSomethingColumn)

我正在寻找解释推理的参考。

I am looking for references explaining the reasoning as well.

我听说有人说技术上 NULL 不必定义为 0 ,但来吧!如果是这样,那么吸盘(我们的应用程序)将在 -2147483648 中以不同的方式崩溃。

I have heard some people say that technically NULL does not have to be defined as 0, but come on! if that was the case, then the sucker (our app) would crash in -2147483648 different ways.

,如果 NULL!= 0 ,那么我们会有很大的问题。

So, if NULL != 0, then we will have big problems.

请帮我解决一个无意义的语法争论。我不是特别依附于任何一种方式;只是寻找官方的方式。谢谢。

Please help me settle a pointless syntax debate. I am not particularly attached to either way; just looking for the official way. Thanks.

我们正在使用Visual Studio C ++编译器。

P.S. We are using Visual Studio C++ compiler.

推荐答案

我认为这是历史的意外,零对应于假和非零对应到真;我喜欢仅保留这些表单的布尔表达式。

I consider it an accident of history that zero corresponds to false and non-zero corresponds to true; I like to reserve those forms for boolean expressions only. However I don't have a problem with seeing that form in someone else's code.

我的投票:

if (pSomethingColumn == NULL)

从来没有被忘记了一个等号。只是幸运,或者可能非常小心。

No, I've never been bitten by forgetting one of the equal signs. Just lucky, or maybe extremely careful.

这篇关于在C ++中检查Null指针的首选方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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