home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,310 of 33,346   
   Francis Glassborow to Ike Naar   
   Re: postfix operator++   
   18 May 12 06:07:11   
   
   From: francis.glassborow@btinternet.com   
      
   On 18/05/2012 02:04, Ike Naar wrote:   
   > On 2012-05-17, ravinder thakur  wrote:   
   >> I am kind of puzzled by one simple c++ constuct. What should be the   
   >> value of x after the statement:   
   >>   
   >> int x = 10;   
   >> x = x++;   
   >>   
   >> I expect value of x to be 10 after the second assignment, however its   
   >> coming out to be 11. Any ideas whats happening ?   
   >   
   > (Nit: there is no second assignment. The first line is not an   
   assignment).   
   >   
   > The value of x equals 10 before the assignment.   
   > If you want x to equal 10 after the assignment as well,   
   > then you actually want the assignment not to change x.   
   > So get rid of the assignment. Problem solved.   
   >   
   >   
      
   Not so:   
      
   void foo(int & i, int & j){   
        i=j++;   
   }   
      
   int main(){   
        int i{10};   
        foo(i, i);   
   // problem occurs   
   }   
      
      
   We cannot simply avoid problem code unless we understand why it is a   
   problem. And note that any reasonable compiler is going to inline foo()   
   resulting in EXACTLY the original code.   
      
   Francis   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca