简体   繁体   中英

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.

The operator && is logical AND , so if res == CCAPI_OK and state != 0 then it will return true . In this case state is being implicitly converted to bool for the && operation.

Operator && is Logical AND , could be written as and too.

The logical operators apply logic functions (NOT, AND, and inclusive OR) to boolean arguments (or types contextually-convertible to bool), with a boolean result.

So the last statement will return the operation result with type bool .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM