-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); }' –