Changing associativity
        Posted  
        
            by Sorush Rabiee
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Sorush Rabiee
        
        
        
        Published on 2010-03-21T15:36:41Z
        Indexed on 
            2010/03/21
            15:41 UTC
        
        
        Read the original article
        Hit count: 404
        
c++
Hi...
The associativity of stream insertion operator is rtl, forgetting this fact sometimes cause to runtime or logical errors. for example:
1st-
int F()
{
   static int internal_counter c=0;
   return ++c;
}
in the main function:
//....here is main()
cout<<”1st=”<<F()<<”,2nd=”<<F()<<”,3rd=”<<F();
and the output is:
1st=3,2nd=2,3rd=1
that is different from what we expect at first look.
2nd- suppose that we have an implementation of stack data structure like this:
    //
    //... a Stack<DataType> class …… 
    //
    Stack<int> st(10);
    for(int i=1;i<11;i++)
       st.push(i);
cout<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl;
expected output is something like:
10
9
8
7
but we have:
7
8
9
10
There is no internal bug of << implementation but it can be so confusing... and finally[:-)] my question: is there any way to change assocativity of an operator by overloading it?
© Stack Overflow or respective owner