ASSERT 宏的使用

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

msdn中是这样描述ASSERT的:ASSERTASSERT( booleanExpression )

Parameters

booleanExpression

Specifies an expression (including pointer values) that evaluates to nonzero or 0.

Remarks

Evaluates its argument. If the result is 0, the macro prints a diagnostic message and aborts the program. If the condition is nonzero, it does nothing.

The diagnostic message has the form

assertion failed in file <name> in line <num>

where name is the name of the source file, and num is the line number of the assertion that failed in the source file.

In the Release version of MFC, ASSERT does not evaluate the expression and thus will not interrupt the program. If the expression must be evaluated regardless of environment, use the VERIFY macro in place of ASSERT.

Note This function is available only in the Debug version of MFC.

Example

// example for ASSERT

CAge* pcage = new CAge( 21 ); // CAge is derived from CObject.

ASSERT( pcage!= NULL )

ASSERT( pcage->IsKindOf( RUNTIME_CLASS( CAge ) ) )

// Terminates program only if pcage is NOT a CAge*.

在开发过程中我们可以假设只要程序运行正确,某一条件肯定成立。如不成立,那么我们可以断言程序肯定出错。

在这种情况下我们可以利用ASSERT来设定断言。ASSERT宏的参数是一个逻辑表达式,在程序运行过程中,若该逻辑表达式为真,则不会发生任何动作,若此表达式为假,系统将弹出一个对话框警告你,并停止程序的执行。同时要求你作出选择:Abort,Ignore, Retry。若你选择Abort,系统将停止程序的执行;若你选择Ignore 系统将忽略该错误,并继续执行程序;若你选择Retry ,系统将重新计算该表达式,并激活调试器。

同TRACE宏一样,ASSERT宏只DEBUG版本中起作用,在RELEASE版本中ASSERT宏将被忽略

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航