开发者

! meaning in c++ (following context)

I know what != is when if(x!=value){...}, but what does it mean in the following context:

 if (! ReadConsoleInput(
                hStdin,      // input buffer handle
                irInBuf,     // buffer to read into
                128,         // size of read buffer
                &cNumRead) ) // number of records read
            ErrorExit("ReadConsoleInput");

or

if (! SetConsoleMode(hStdin, fdwMode) )
        ErrorExit("SetConsoleMode");

开发者_如何转开发or

 if (! GetConsoleMode(hStdin, &fdwSaveOldMode) )
        ErrorExit("GetConsoleMode");


It's the "Not" operator: true (1) if the operand is zero, false (0) otherwise.


! is LOGICAL NOT, i.e.: if (! boolVar) equals to if (true != boolVar), and if (! intVar) equals to if (0 == intVar)

If you have a function foo() that returns 0 on error, checking if (! foo()) is basically checking if the function succeeded or not, enter brackets on failure.

You have, of course, to know exactly the return values policy for each function, there's no law or rule about it.


! means "logical-not"... it inverts the boolean sense of the following value (i.e. tests the following value is false). If necessary, the following value will be converted to a boolean first: numeric/pointer values other than 0 are true, 0 (NULL) is false; classes can provide a conversion operator that will provide either a bool or a numeric/pointer type convertible to bool. Standard-compliant compilers even let you write the functionally identical code:

if (not xyz...)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜