请问高手:ASSERT的用法是什么,我只知道它是用来诊断的。但我不理解“诊断”的含义,清高手指点。先谢了。

解决方案 »

  1.   

    ASSERT(expression)
    当表达式expression为真时,此语句相当于没有存在,为假时弹出assert failed 对话框,用来保证expression为真
      

  2.   

    如果 你在ASSERT()中的语句出现非法,它会给你提示的
      

  3.   

    有错误时,就有窗口跳出来,不会执行下去的
    如ASSERT(a)
    如果a=0,就不下去了,可以保证a!=0
      

  4.   

    可以判断指针是否为空,hwnd 是否合法等等。
      

  5.   

    就是用于断定。在MSDN里有详细的说明
      

  6.   

    谢谢各位热心的解答:。那ASSERT一般用在哪里?比如要判断一个条件用 if...else 不是可以了吗? 当条件不满足时,此是用 ASSERT ,我不用ASSERT,用AfxMessageBox(); 来提示用户,可以吗?用ASSERT比用AfxMessageBox()的优势在哪里?请高手指点。先谢了。
      

  7.   

    //注意,这个断言只在DEBUG状态下有效,如改成发行版它将会和它判断的语句
    //一起消失,而VERIFY只会消失自己,不会将它的语句消失。
    ASSERT
    ASSERT( booleanExpression )ParametersbooleanExpressionSpecifies an expression (including pointer values) that evaluates to nonzero or 0.ResEvaluates 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 formassertion 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*.
      

  8.   

    Assert(expression);
    用来判断该expression是不是成立,若不成立则会跳出一个异常对话框。因为在你开始一段程序时候,expression是一个你默认成立的条件,它如果不成立的话,对于你程序的运行将可能是致命的,所以你应该先确认一下你要求的条件是否成立,以保证你程序的健壮性。
      

  9.   

    都回答的很好啊!小弟同问一下ASSERT和VERIFY之间有什么区别和联系啊?
    是不是就如glassshark(追赶蜗牛)兄所言的那样的?仅仅是在debug和release条件下的对前者相当于失效,而后者仍旧起作用?
      

  10.   

    它与原来在DOS下时的TC或者BC中的assert()库函数功能几乎完全一样!
      

  11.   

    我也问一下ASSERT和VERIFY之间有什么区别和联系啊?
      

  12.   

    glassshark(追赶蜗牛) 最精典的回话:"ASSERT与VERIFY只在DEBUG状态下有效,
    如改成发行版(Release)它将会和它判断的语句一起消失,而VERIFY只会消失自己,不会将它的语句消失。"所以兄弟们编程时不要为了省事而用它!该用if时就用if!
      

  13.   

    谢谢各位高手热心的帮助。那什么时候用DEBUG什么时候用Release。他门两者有什么区别:各有什么优势。清高手指点。