evaulation of boolean conditions...


J

John Ratliff

Does C++ employ lazy evaluation of boolean conditions, e.g. can I do the
following safely

if ((obj != NULL) || (obj->someMethod())) {
// blah blah
}

Or will I get a segfault if the obj ptr is NULL? This is fine in Java, but I
wasn't sure about C++.

Thanks,

--John Ratliff
 
Ad

Advertisements

J

John Ratliff

I meant &&, not || in my condition.

if ((obj != NULL) && (obj->someMethod())) {
// blah blah
}
 
N

Nirmalya Ghosh Chowdhury

Yes, C++ uses short circuit OR, i.e If the first expression turns out
to be false , it wont evaluate further expressions.
- Nirmalya
 
U

upashu2

In C++The first operand ( a) is completely evaluated and all side effects are
completed before continuing evaluation of the logical AND expression.

The second operand (b) is evaluated only if the first operand
evaluates to true (nonzero). This evaluation eliminates needless
evaluation of the second operand when the logical AND expression is
false. You can use this short-circuit evaluation to prevent
null-pointer dereferencing, as shown in the following example:
..
if ((obj != NULL) && (obj->someMethod()))
If obj is null (0), the right side of the expression is never
evaluated.
 
S

Srini

Does C++ employ lazy evaluation of boolean conditions, e.g. can I do the
following safely
if ((obj != NULL) || (obj->someMethod())) {
// blah blah
}

Yes. If obj is NULL, the second part of the conditional will not be
evaluated.

Regards,
Srini
 
Ad

Advertisements

A

ambar.shome

I think this is OK.
Provided you have instantiated your class in this way using new
operator

Obj *obj = new Obj();
 
G

Greg

upashu2 said:
In C++
The first operand ( a) is completely evaluated and all side effects are
completed before continuing evaluation of the logical AND expression.

The second operand (b) is evaluated only if the first operand
evaluates to true (nonzero). This evaluation eliminates needless
evaluation of the second operand when the logical AND expression is
false. You can use this short-circuit evaluation to prevent
null-pointer dereferencing, as shown in the following example:
.
if ((obj != NULL) && (obj->someMethod()))
If obj is null (0), the right side of the expression is never
evaluated.

But there's a catch.

The correct answer is that righthand side of the && expression in the
sample code provided ( (obj->someMethod()))) may or may not be
evaluated even when objc != NULL is false. There is not enough in the
sample provided to answer for certain.

Only the built-in && operator is guaranteed not to evaluate the
righthand side of the expression if the lefthand side is false. A
user-defined && operator would always force an evaluation of the
righthand side of its expression since it is passed as a parameter to
the operator&& routine.

Since the code above omits obj's declaration, the && operator (and the
!= operator as well) could have been overloaed in the case that obj is
declared as some kind of a smart pointer type.

Greg
 
G

Geo

Nirmalya said:
Yes, C++ uses short circuit OR, i.e If the first expression turns out
to be false , it wont evaluate further expressions.
- Nirmalya

I think you meant AND, OR will will stop at the first 'true' expression.
 
Ad

Advertisements

M

msalters

(e-mail address removed) schreef:
I think this is OK.
Provided you have instantiated your class in this way using new
operator

Obj *obj = new Obj();

No, it doesn't matter how it was created. The following is also OK

void foo( Obj* obj )
{
if( Obj && Obj->SomeBool )
// ....
}
void bar( )
{
Obj o;
foo( &o );
}

BTW, you know that (new Obj) never returns 0? If it fails it throws
std::bad_alloc so you don't have to test it.

HTH
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top