In the example below, what exactally is the << operator doing? I’m guessing it is not a bitwise operator.
std::cout << "Mouse down @ " << event.getPos() << std::endl;
I understand what the code will do here: Use standard out, send this text, send an end of line. Just I’ve never come accross the use of this << apart from on raw binary.
I’m starting out with C++. And, as an operator of sorts, it’s hard to search for a description of this and what it means. Can someone enlighten me and/or give me a pointer as to what to google for?
Thanks
Ross
The answer is: The
<<operator does left shifts by default for integral types, but it can be overloaded to do whatever you want it to!This syntax for piping strings into a stream was first (I think) demonstrated in C++ inventor Bjarne Stroustroup’s eponymous book The C++ Programming Language. Personally, I feel that redefining an operator to do IO is gimmicky; it makes for cool-looking demo code but doesn’t contribute to making code understandable. Operator overloading as a technique has been widely criticized in the programming language community.
EDIT: Since nobody else has mentioned this yet:
operator<<is defined in theostreamclass, of whichcoutis an instance. The class definition sits in the iostream library, which is#include‘d as<iostream>.