|
本帖最后由 liucqa 于 2013-7-30 12:05 编辑
VB6是不支持条件短路的,VB6的编译器不提供此运算功能。当在做逻辑运算时,如:And,Or运算时,无论And的前半部分条件是真还是假,照旧需要运算后半部分的结果,而后将两个结果再进行一次比较运算。
**************************************************************************************************************************
不过在VB.Net中可以支持条件短路运算,但需要使用VB.Net提供的新的运算符AndAlso和OrElse,VB.Net中的AndAlso相当于C风格语言的&&运算,同理VB.Net的OrElse相当于C风格语言的||运算。
C#中的两组逻辑运算符&&和||支持短路运算
逻辑运算的短路原则是C语言标准明确规定了的,必须按照自左至右的原则
ANSI C 规定了逻辑操作符都是短路求值的。对逻辑与,标准时这么说的,注意粗体部分内容:
6.5.13 Logical AND operator
Syntax
1 logical-AND-expression:
inclusive-OR-expression
logical-AND-expression && inclusive-OR-expression
Constraints
2 Each of the operands shall have scalar type.
Semantics
3 The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it
yields 0. The result has type int.
4 Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation;
there is a sequence point after the evaluation of the first operand. If the first operand
compares equal to 0, the second operand is not evaluated.
要小心千万不要重载 || 、&&运算符,因为一旦重载,则编译器就无法保证短路了。事实上重载这些运算符是一种相当糟糕应该被杜绝的做法。
短路规则:逻辑与和逻辑或运算都是按照左边进行短路运算
所以大家可以放心使用
if(NULL=pMsg||NULL==pMsg->pData)这样的逻辑判断
|
|