我们可以将逻辑表达式的结果分配给整数变量吗?

Can we assign the result of a logical expression to an integer variable?

我搜索了这个问题,但我找到的答案是逻辑表达式的结果不能分配给整数变量它只能分配给布尔变量,但后来我尝试了一个简单的代码来检查我的指出结果可以分配给整数变量,因为首先逻辑表达式 returns 是一个整数值。

#include <iostream>
using namespace std;
int main() {
    int n1 = 1;
    int n2 = 2;
    int b;
    b = (n1 == n2);
    cout<<b;
}

基本上是这种情况,逻辑表达式的结果是假的,这意味着它是 0

So why is it wrong to store the result in an integer value??

Here's one of the websites Here's another one

当你说:

b = (n1 == n2);

编译器说:

b = int(n1 == n2);

这个概念被称为隐式类型转换。