When does operator <<
refer to the insertion operator and when does it refer to the bitwise left shift?
This will output 10
, and operator <<
refers to the left shift.
cout << a.b() << a.a.b << endl;
And this will output 11
, operator <<
refers to the insertion operator.
cout << a.b();
cout << a.a.b ;
I am confused, when will operator <<
(when use with cout
) refer to the left shift operator?
#include <iostream>
using namespace std;
class A {
public:
A() { a.a = a.b = 1; }
struct { int a, b; } a;
int b();
};
int A::b(){
int x=a.a;
a.a=a.b;
a.b=x;
return x;
};
int main(){
A a;
a.a.a = 0;
a.b();
cout << a.b() << a.a.b << endl; // ?????
return 0;
}
int
like types. This is a built-in facility. If<<
is overloaded, then it can be used for other purposes. – Frissewhen does operator << refer to insertion operator and when it refer to bitwise left shift ? (c++)
When the operand types, subject to precedence/associativity, clearly tell the language which overload to use. – Selfinductance10
was the result of bit-shifting? – Nebiim