bool 和BOOL的区别
来源:互联网 发布:淘宝客推广有什么要求 编辑:程序博客网 时间:2024/06/04 19:13
msdn上查到的
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':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.Code:typedef int BOOL;#ifndef FALSE#define FALSE 0#endif#ifndef TRUE#define TRUE 1#endif
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'?A: Actually no, it is not OK. If you read the documentation for APIs you can see that for most of them:Code:BOOL ret = SomeSDKFunction();if(TRUE == ret){ // do something}
'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.Originally Posted byMSDN
Here are several examples:This function returns 4 possible values.Code:BOOL EnableMenuItem(HMENU hMenu, UINT uIDEnableItem, UINT uEnable );
Originally Posted byMSDNCode:BOOL GetMessage(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax );
This is another example that other values than 0 and 1 can be used as a 'BOOL'.Originally Posted byMSDN
So you should code it like: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 = SomeWinAPI();if(ret){ // do something}
Code:BOOL ret = GetMessage(...);if(ret == 0){ // WM_QUIT}else if(ret == -1){ // error}else{ // success}
Last edited by Andreas Masur; July 24th, 2005 at04:59 PM.
0 0
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL 和 bool 的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL和bool的区别
- bool 和BOOL的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL和bool的区别
- BOOL和bool的区别
- Bool 和 BOOL 的区别
- 手机更新失败丢失文件怎么恢复
- 从子界面获取数据返回给主界面
- 老字号,没那么容易倒——wifi共享精灵
- U盘文件被损坏如何才能恢复
- 用mysql数据库写的分词算法代码
- bool 和BOOL的区别
- 读取笔记本的摄像头的原始yuv数据,通过libav(ffmpeg编码)
- Git 过滤文件,控制上传
- 不常用到的sql语句记录
- yii上传图片、yii上传文件、yii控件activeFileField使用
- vi
- 对象左列表增加非对象关联表的字段,添加两个以上字段
- plsql developer中 保存/删除 登录的用户名和密码
- 不定期搬运二次元福利