定义双重感叹号? [英] Defining double exclamation?
问题描述
我了解双感叹号的作用(或者我想我了解),但是我不确定如何在随机对象上定义它.例如,在下面的代码段中:
I understand what a double exclamation mark does (or I think I understand) but I am not sure how it is defined on a random object. For example in the code snippet below:
Assignment *a;
if (!getAssignment(query, a))
return false;
hasSolution = !!a;
if (!a)
return true;
我怎么知道双感叹号将产生什么值?换句话说,它总是转换为true吗?错误的 ?还是可以为其定义行为,例如执行确定结果的方法(对象如何知道在这种情况下如何行动)?由于发生了所有这些感叹号,我对这段代码有些困惑.
How do I know what value will the double exclamation mark result in ? In other words does it always convert to true ? false ? or can you define a behavior for it such as executing a method to determine the result (how does the object know how to act in this situation) ? I am bit confused about this piece of code due to all these exclamation stuff going on.. Any explanation is appreciated.
希望我很清楚,谢谢.
推荐答案
a
是一个指针.在C ++中,nullptr
被定义为无效的指针. !pointer
将nullptr
指针变为true
,将非nullptr
指针变为false
. !boolean
将true
变成false
,将false
变成true
.它将始终有效.
a
is a pointer. In C++, nullptr
is defined to be an invalid pointer. !pointer
turns a nullptr
pointer into true
and a non nullptr
pointer into false
. !boolean
turns true
into false
and false
into true
. It will always work.
!(!a)
是思考它的一种有用方法.
!(!a)
is a useful way to think of it.
这篇关于定义双重感叹号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!