|
-
May 14th, 2007, 02:52 PM
#16
Re: A question for C++ expert on ++a and a++
 Originally Posted by TheCPUWizard
Code:
if ((ptr) && (ptr->MethodIO))
{
}
i always make code like this:
Code:
if (ptr)
if(ptr->MethodIO)
{
}
i dont think its any diffrent..
however the evaluation stops at ptr, is this defined behavior by the standards?
-
May 14th, 2007, 03:24 PM
#17
Re: A question for C++ expert on ++a and a++
The standard guarantees that the right-hand-side of && will not be evaluated if the left-hand side of && is false. Also the right-hand-side of || will not be evaluated if the left-hand side of && is true.
But if && or || is overloaded, that guarantee is lost because both sides need to be evaluated before operator&& or operator|| can be called, I think.
-
May 14th, 2007, 09:38 PM
#18
Re: A question for C++ expert on ++a and a++
 Originally Posted by exterminator
the link provided by exterminator explain one more factor, a++ and ++a can be evaluated at the same time, which can mess up the memory. so maybe undefined behavior will be more correct than unspecified behavior.
Thanks!
-
May 14th, 2007, 10:48 PM
#19
Re: A question for C++ expert on ++a and a++
 Originally Posted by Mitsukai
however the evaluation stops at ptr, is this defined behavior by the standards?
Yes, it is.
Code:
5.14 Logical AND operator [expr.log.and]
logical-and-expression:
inclusive-or-expression
logical-and-expression && inclusive-or-expression
1 The && operator groups left-to-right. The operands are both implicitly
converted to type bool (clause 4). The result is true if both operands are
true and false otherwise. Unlike &, && guarantees left-to-right evaluation:
the second operand is not evaluated if the first operand is false.
2 The result is a bool. All side effects of the first expression except for
destruction of temporaries (12.2) happen before the second expression is evaluated.
&& also is a sequence point.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|