Changing associativity
- by Sorush Rabiee
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?