[英]null pointer in C++
everyone, I have some question about C++, what do You actually prefer to use 大家好,我对C ++有一些疑问,你真的喜欢用什么
int* var = 0;
if(!var)...//1)
or
if(var == 0)..//2)
what are the pros and cons? 优缺点都有什么? thanks in advance
提前致谢
我更喜欢if (!var)
,因为那时你不会意外地分配给var
,而你不必在0
和NULL
之间选择。
I've always been taught to use if (!var)
, and it seems that all the idiomatic C(++) I've ever read follows this. 我一直被教导使用
if (!var)
,似乎我读过的所有惯用的C(++)都遵循这一点。 This has a few nice semantics: 这有一些很好的语义:
var
does not exist" or "If var is not set" var
不存在”或“如果var未设置” var
was a boolean (bools aren't available in C, but people still mimic their use) var
是布尔值,那么映射与你惯用的内容密切相关(bool在C中不可用,但人们仍然模仿它们的使用) if (!isEmpty(nodeptr)) { .. }
Mostly personal preference. 主要是个人偏好。 Some will advise that the first is preferred because it becomes impossible to accidentally assign instead of compare.
有人会建议第一个是首选,因为不可能意外分配而不是比较。 But by that same token, if you make a habit of putting the rvalue on the left of the comparison, the compiler will catch you when you blow it:
但是同样的道理,如果你养成了将rvalue放在比较左边的习惯,编译器会在你吹它时抓住你:
if( 0 == var )
...which is certainly true. ......这当然是真的。 I simply find
if( !var )
to be a little more expressive, and less typing. 我只是发现
if( !var )
是否更具表现力,更少打字。
They will both evaluate the same, so there's no runtime difference. 他们都会评估相同,所以没有运行时差异。 The most important thing is that you pick one and stick with it .
最重要的是你选择一个并坚持下去 。 Don't mix one with the other.
不要将一个与另一个混合。
A few years has passed... 几年过去了......
The C++11 standard introduced nullptr
to check for null pointers and it should be used as it ensure that the comparison is actually done on a pointer. C ++ 11标准引入了
nullptr
来检查空指针,应该使用它,因为它确保比较实际上是在指针上完成的。 So the most robust check would be to do: 所以最强大的检查是:
if(myPtr != nullptr)
The problem with !var
is that it's valid if var is a boolean or a numerical values (int, byte etc...) it will test if they are equal 0 or not. !var
的问题在于,如果var是布尔值或数值(int,byte等......),它将测试它们是否等于0。 While if you use nullptr
, you are sure that you are checking a pointer not any other type: 如果你使用
nullptr
,你确定你正在检查指针而不是任何其他类型:
For example: 例如:
int valA = 0;
int *ptrA = &valA;
if(!valA) // No compile error
if(!*ptrA) // No compile error
if(!ptrA) // No compile error
if(valA != nullptr) // Compile error, valA is not a pointer
if(*ptrA != nullptr) // Compile error, *ptrA is not a pointer
if(ptrA != nullptr) // No compile error
So, it's pretty easy to make a mistake when manipulating pointer to an int as in your example, that's why nullptr
should be used. 因此,在示例中操作指向int的指针时很容易出错,这就是为什么应该使用
nullptr
。
Either one is good, though I personally prefer 2 - or something like it. 任何一个都是好的,虽然我个人更喜欢2 - 或类似的东西。
Makes more sense to me to read: 让我更有意义阅读:
if ( ptr != NULL )
than 比
if ( ptr )
The second I may confuse for just being a boolean to look at, but the first I'd be able to tell immediately that it's a pointer. 第二个我可能会因为只是一个布尔值而混淆,但第一个我能够立刻告诉它它是一个指针。
Either way, I think it's important to pick one and stick with that for consistency, though - rather than having it done in different ways throughout your product. 无论哪种方式,我认为重要的是选择一个并坚持这一点以保持一致性 - 而不是在整个产品中以不同的方式完成它。
I would prefer option 3: 我更喜欢选项3:
if(not var)
{
}
The new (well since 1998) operator keywords for C++ can make code easier to read sometimes. C ++的新(自1998年以来)运算符关键字有时可以使代码更容易阅读。
Strangely enough, they seem to be very unknown. 奇怪的是,它们似乎很不为人知。 There seem to be more people who know what trigraphs are (thank you IOCCC!), than people who know what operator keywords are.
似乎有更多的人知道什么是三字母(谢谢IOCCC!),而不是那些知道运算符关键字的人。 Operator keywords have a similar reason to exist: they allow to program in C++ even if the keyboard does not provide all ANSII characters.
运算符关键字存在类似的原因:即使键盘不提供所有ANSII字符,它们也允许使用C ++编程。 But in contradiction to trigraphs, operator keywords make code more readable instead of obfuscating it.
但与trigraphs相反,运算符关键字使代码更具可读性而不是混淆代码。
According to the High Integrity C++ coding Standard Manual: for comparison between constants and variables you should put the constant on the left side to prevent an assignment instead of an equality comparison , for example: 根据高完整性C ++编码标准手册: 为了比较常量和变量,您应该将常量放在左侧以防止赋值而不是相等比较 ,例如:
Avoid:
if(var == 10) {...}
Prefer
if(10 == var) {...}
Particularly in your case I prefer if(0 == var) because it is clear that you are comparing the pointer to be null (0). 特别是在你的情况下,我更喜欢if(0 == var),因为很明显你将指针比较为null(0)。 Because the !
因为! operator can be overloaded then it could have other meanings depending on what your pointer is pointing to: if( !(*var) ) {...} .
运算符可以重载然后它可能有其他含义,具体取决于指针指向的内容: if(!(* var)){...} 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.