C ++逻辑&运算符 [英] C++ logical & operator

查看:147
本文介绍了C ++逻辑&运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有逻辑&运算符在C ++?例如作为&&&除了它还评估稍后的参数,即使一些先前的参数已经评估为false吗?操作员&是我理解的按位和运算符。

Is there a logical & operator in C++? e.g. an operator that works just as && except that it also evaluates later arguments even if some preceding ones have already evaluated to false? The operator & is the bitwise and operator I understand.

推荐答案

确实是按位运算符。我假设你有类似

The operator & is indeed the bitwise operator. I'm assuming you have something like

if ( f() && g() ) { /*do something*/ }

并且您希望执行f()和g的人被评价为假。我建议你做别的事情:

and you want both f() and g() to execute, regardless of whether one of them was evaluated to false. I suggest you do something else instead:

bool bF = f();
bool bG = g();

if ( bF && bG ) { /*do something*/ }


$ b b

这也提供了更好的可读性,不会混淆其他尝试维护代码的程序员。从长远来看,这是值得的。

This also provides better readability and doesn't confuse other programmers who try to maintain your code. In the long run, it's worth it.

这篇关于C ++逻辑&运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆