'BOOL' 和 'bool' 的区别?
2014-10-23 00:13
316 查看
原文:http://forums.codeguru.com/showthread.php?332831-Visual-C-General-What-is-the-difference-between-BOOL-and-bool
Visual C++ General: What is the difference between 'BOOL' and 'bool'?
Q: What is the difference between 'BOOL' and 'bool'?A: 'bool' is a built-in C++ type while 'BOOL' is a Microsoft specific type that is defined as an 'int'. You can find it in 'windef.h':
Code:
typedef int BOOL; #ifndef FALSE #define FALSE 0 #endif #ifndef TRUE #define TRUE 1 #endif
The only possible values for a 'bool' are 'true' and 'false', whereas for 'BOOL' you can use any 'int' value, though 'TRUE' and 'FALSE' macros are defined in 'windef.h' header.
Q: What is the size of 'BOOL' and 'bool'?
A: If you use the 'sizeof' operator, it will yield 1 for 'bool', though according to the standard the size of' bool' is implementation defined, and 4 for 'BOOL', on 32-bits platform, where 'sizeof(int)' is 4 bytes. If the size of 'int' changes
to 8 bytes on 64-bits platforms, 'sizeof(BOOL)' will yield 8 instead.
Q: Does 'BOOL' has to do with MFC?
A: 'BOOL' was used by Microsoft long before 'bool' was actually added to the C++ language, but it has nothing to do with MFC. Many Windows API returns a 'BOOL' to indicate success or failure.
Q: Is it OK if I test the return of a Windows SDK function against 'TRUE'?
Code:
BOOL ret = SomeSDKFunction(); if(TRUE == ret) { // do something }
A: Actually no, it is not OK. If you read the documentation for APIs you can see that for most of them:
Originally Posted byMSDN
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError().
'TRUE' is defined as 1, but nothing guarantees that the function actually returns 'TRUE' (a value that is nonzero) or some other value. In fact there are functions that indeed return multiple values like –1, 0 or 1.
Here are several examples:
Code:
BOOL EnableMenuItem(HMENU hMenu, UINT uIDEnableItem, UINT uEnable );
This function returns 4 possible values.
Originally Posted byMSDN
The return value specifies the previous state of the menu item (it is either MF_DISABLED, MF_ENABLED, or MF_GRAYED). If the menu item does not exist, the return value is -1.
Code:
BOOL GetMessage(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax );
Originally Posted byMSDN
If the function retrieves a message other than WM_QUIT, the return value is nonzero.
If the function retrieves the WM_QUIT message, the return value is zero.
If there is an error, the return value is -1.
This is another example that other values than 0 and 1 can be used as a 'BOOL'.
So you should code it like:
Code:
BOOL ret = SomeWinAPI(); if(ret) { // do something }
And for the functions that are know to return values like -1 you also must take that into account. For instance in the case of 'GetMessage()':
Code:
BOOL ret = GetMessage(...); if(ret == 0) { // WM_QUIT } else if(ret == -1) { // error } else { // success }
相关文章推荐
- 'isEqua' 和 '==' 的区别
- "".equals(str)和str.equals('"")的区别
- " "(双引号)与 ' '(单引号)的区别 (转载)
- BOOL、bool的区别 && NULL、0、nullptr的区别
- " "(双引号)与 ' '(单引号)的区别
- '\r' 与 '\n'的区别
- 'scalar deleting destructor' 和 'vector deleting destructor'的区别
- 'physical corruption' 和'logical corruption' 的区别
- 【未解决】php 中 '' "" null empty的区别
- 'alter system switch logfile' 和 'alter system archive log current'的区别
- '' 和 "" 区别
- 我理解keso的"东拉西扯:不要听用户的" -对牛乱弹琴 | Playin' with IT
- 解决:服务器无法加载应用程序 '/LM/W3SVC'。错误是 '找不到指定的元数据'。
- 在php中"RE:$message" 与 'RE:$message' 区别
- 'NULL' undeclared错误
- &&与&区别
- fw:请问Null和空字符串('')的区别?
- 'Sys' is undefined 错误 -- 使用ASP.NET 2.0 AJAX Extensions
- 'com.sun.tools.javac.Main',not found的解决办法
- JTree展开结点设置为'+'"-"形式