读书人

帮忙解释一个断言的宏定义解决方案

发布时间: 2012-01-13 22:43:29 作者: rapoo

帮忙解释一个断言的宏定义
#define ASSERT (f)
do{
if (!(f) && AfxAssertFailedLine(THIS_FILE, __LINE__)) AfxDebugBreak()
} while (0) ;
有个例子是上面这样写的
是这样用的
assert( (pvTo != NULL ) && ( pvFrom != NULL )) ;

不明白的是:
1.明明用的是小写的assert,怎么宏定义成ASSERT呢
2.宏定义中那个dowhile循环不是就做一次吗,为什么还要定义成只做一次的循环
3.我在msdn里怎么找不到AfxAssertFailedLine的帮助呢
4.#include "stdafx.h "中的afx是哪个单词的缩写
谢谢大家了.


[解决办法]
1 因为是两个宏
2 应该是作用域的问题吧
3 不知道
4 这个似乎在深入浅出mfc中提到过。
  AFX is an abbreviation for a group. The MFC group was originally called the "Application Framework group " during the inception of MFC. However, X doesn’t stand for anything other than sounding cool. The original AFX group was renamed to MFC long ago. There is no current AFX group. But the "AFX " hasn’t been changed to "MFC " for the compatibility.
[解决办法]
ASSERT是微软编译器扩充的宏;
assert是C++语言函数。
[解决办法]
AfxAssertFailedLine函数在afxasert.cpp 文件里。代码有注释

BOOL AFXAPI AfxAssertFailedLine(LPCSTR lpszFileName, int nLine)
{
#ifndef _AFX_NO_DEBUG_CRT
// we remove WM_QUIT because if it is in the queue then the message box
// won 't display
MSG msg;
BOOL bQuit = PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE);
#ifndef _WIN32_WCE
BOOL bResult = _CrtDbgReport(_CRT_ASSERT, lpszFileName, nLine, NULL, NULL);
#else
USES_CONVERSION;
BOOL bResult = _CrtDbgReportW(_CRT_ASSERT, A2W(lpszFileName), nLine, NULL, NULL);
#endif
if (bQuit)
PostQuitMessage((int)msg.wParam);
return bResult;
#else
// Not supported.
#error _AFX_NO_DEBUG_CRT is not supported.
#endif // _AFX_NO_DEBUG_CRT
}

读书人网 >VC/MFC

热点推荐