-1
我有以下代码,我不确定它应该是== TRUE
还是!= FALSE
。WinAPI AttachConsole?
这是现在的代码:
void AttachConsole() {
bool has_console = ::AttachConsole(ATTACH_PARENT_PROCESS) == TRUE;
if (!has_console) {
// We weren't launched from a console, so just return.
// We could alloc our own console, but meh:
// has_console = AllocConsole() == TRUE;
has_console_attached_ = false;
return;
}
has_console_attached_ = true;
}
我觉得应该是!= FALSE
,但我不知道?
在windows中,TRUE是1且FALSE是0,所以你所考虑的两种方法是相同的 – bruceg
TRUE可能是1,但是大多数BOOL函数被记录为返回非零在成功的时候,不是特别的,最好使用'!= FALSE'。在这种情况下,任何非零值在分配给“bool”时都会转换为“true”,所以根本不要比较这个值,就像Jerry的答案所示。 –
@ThomasMccaffery:你知道这个例子的功能可以简化为一行代码吗? 'void AttachConsole(){has_console_attached_ = :: AttachConsole(ATTACH_PARENT_PROCESS); }' –