I have difficulties in understanding the sequence of calls in the code below. I was expecting to see the output below
A1B2
While I can see that the output I get is
BA12
I thought that the call std::cout<< b->fooA() << b->fooB() << std::endl
was equivalent to call
std::cout.operator<<( b->fooA() ).operator<< ( b->fooB() )
but I can see that this is not the case. Can you help me understanding better how this does it work and the relationship with the global operator<<
? Is this last ever called in this sequence?
#include <iostream>
struct cbase{
int fooA(){
std::cout<<"A";
return 1;
}
int fooB(){
std::cout <<"B";
return 2;
}
};
void printcbase(cbase* b ){
std::cout << b->fooA() << b->fooB() << std::endl;
}
int main(){
cbase b;
printcbase( &b );
}
AB12
orBA12
are possible outputs.A1B2
is not (hopefully). – Airborne