[英]C++ Function Return with && Operator
I'm looking through a library, and I see this function: 我正在浏览一个库,看到了以下功能:
bool CCAPI::IsConnected() const
{
int state;
int res = CCAPIGetConnectionStatus(&state);
return (res == CCAPI_OK) && state;
}
Specifically, what does this last line mean? 具体来说,这最后一行是什么意思? It looks to me like it's returning two variables as it's using the
&&
operator. 在我看来,它正在使用
&&
运算符返回两个变量。 So what's going on here? 那么这是怎么回事?
It is going to return a single bool
, like the function says it will. 它将返回一个
bool
,就像函数说的那样。
The operator &&
is logical AND
, so if res == CCAPI_OK
and state != 0
then it will return true
. 运算符
&&
是逻辑AND
,因此,如果res == CCAPI_OK
且state != 0
则它将返回true
。 In this case state
is being implicitly converted to bool
for the &&
operation. 在这种情况下,
state
将被&&
操作隐式转换为bool
。
Operator &&
is Logical AND , could be written as and
too. 运算符
&&
为逻辑AND ,也可以写为and
。
The logical operators apply logic functions (NOT, AND, and inclusive OR) to boolean arguments (or types contextually-convertible to bool), with a boolean result.
逻辑运算符将逻辑函数(NOT,AND和内含OR)应用于布尔参数(或上下文可转换为bool的类型),并得到布尔结果。
So the last statement will return the operation result with type bool
. 因此,最后一条语句将返回类型为
bool
的运算结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.