CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: A question for C++ expert on ++a and a++

    Quote 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?

  2. #17
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    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.
    My hobby projects:
    www.rclsoftware.org.uk

  3. #18
    Join Date
    Jun 2002
    Posts
    137

    Re: A question for C++ expert on ++a and a++

    Quote Originally Posted by exterminator
    Did you read any of the posts in this thread above before posting?

    //Btw a similar issue came up on clc++m recently - http://groups.google.co.uk/group/com...b9275faa0ec230
    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!

  4. #19
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: A question for C++ expert on ++a and a++

    Quote 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.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured