I’ve been wondering, if i have a function like:
bool Foo::Bar()
{
return A() || B() || C();
}
If A() returns true, would Bar() imediately return true or would it still calculate the results of B() and C() before evaluating the final value?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If
A()is true, neither B nor C will be evaluated. This is called operator short-circuiting.Similarly, in a statement such as
A && B && C, ifAis false, neither B or C will be evaluated.This is not just an optimization; it is especially useful when
BorCdepend onA. For example, you can both test that a pointer points to something and invoke a method on the object being pointed to in a single statement: