它是可以接受的'NULL'分配给布尔数据类型? [英] Is it acceptable to assign 'NULL' to a boolean datatype?
问题描述
它是可以接受的'NULL'分配给布尔数据类型?
Is it acceptable to assign 'NULL' to a boolean datatype?
推荐答案
从理论的角度来看,是的。但它做了可怕的事情。
From a theoretical point of view, yes. But it's a horrible thing to do.
NULL
是分配给一个指针,它指向任何一个空指针常量。
NULL
is a null pointer constant that is assigned to a pointer to make it point to nothing.
...
ptr = NULL; // now it points to no object anymore
...
// or ptr == 0
if(ptr == NULL) {
...
}
下面是标准的参考,如果你有兴趣在任何情况下。首先,空指针常数( 4.10 / 1
)
Here are the references to the Standard if you are interested in any case. First, a null pointer constant is (4.10/1
)
一个空指针常量是一个整型常量前pression计算结果为零整数类型(5.19)右值。
A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero.
那么,如果我们把一个空指针常量布尔
会发生什么?它在解释 4.12 / 1
:
Then, what happens if we convert a null pointer constant to bool
? It's explained in 4.12/1
:
算术,枚举,指针,或指针到成员类型右边的值可以转换为bool类型的右值。零值,空指针值或空成员指针值转换为false;一个
An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; a
(当谈到右值
这基本上意味着相对于该类型的变量一个简单的值)。
(When it talks about rvalue
it essentially means a simple value as opposed to a variable of that type).
现在,什么是真正的是的是 NULL
?阅读 18.1 / 4
(请注意,在C,一个空指针常量定义不同。这可能是它明确提到C ++的原因)
Now, what actually is that NULL
? Read 18.1/4
(note that in C, a null pointer constant is defined differently. Which might be the reason it explicitly refers to C++)
宏NULL是本国际标准实现定义的C ++空指针常量
The macro NULL is an implementation-defined C++ null pointer constant in this International Standard
这是组合的最重要的一点是部分零值...被转换为false
。 NULL
的一个bool变量的赋值将尝试 NULL
转换为布尔值。作为上述段落说,这样的转换存在,并且可以做到的。
The important bit of that combination is the part "A zero value ... is converted to false
". An assignment of NULL
to a bool variable will try to convert NULL
to a boolean value. As the above passages say, such a conversion exist and can be done.
有关空指针了解另一个重要的事情是空指针
和之间的不同的空指针常量
。正如我们刚才读,空指针常量是一些整数值,这就是零。然而,空指针
和它的值,空指针值
,是指针和它们的类型是指针类型。下面有int类型,是一个空指针常量
Another important thing about null pointers to understand is the different between a null pointer
and a null pointer constant
. As we just read, a null pointer constant is some integer value that's zero. However, a null pointer
and its value, a null pointer value
, are pointers and their type is of pointer type. The following has type int and is a null pointer constant
#define NULL ('n'-'n') // looks suspicious, but conforms
由于它是一个整型常量前pression(即本质上是在编译时已知的整数值)与零值。下面是一个空指针值
Because it is a integral constant expression (that is essentially an integer value that is known at compile time) with value zero. The following is a null pointer value
(void*)NULL
但它的不的空指针常量。但无论如何,也空指针的值的被转换为bool作为上述报价告诉: A ..空指针值..被转换为false
。所以,你无一不精。
but it is not a null pointer constant. But anyway, also null pointer values are converted to bool as the above quote tells: "A .. null pointer value .. is converted to false
". So you are all fine.
这篇关于它是可以接受的'NULL'分配给布尔数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!