views:

205

answers:

6

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

A: 

It is a "bitwise left shift" operator.

n << p

Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions. 3 << 2 is 12.

In the context of the question it pushes something into 'cout' which is the current output stream.

Nick Brooks
-1 Not in this context...
Nick Meyer
@Nick Brooks: I've edited your last line to make it a bit clearer, but it's probably too late to save your answer from the hate it's getting.
Carl Smotricz
+4  A: 

The operator<< is being overloaded. Check out Operator Overloading.

Exception
+4  A: 

This is sometimes called the 'stream insertion operator', and this is the most common use: to insert data into a stream. Sometimes, however, I've seen it overloaded to insert data into other objects when doing things like serialization, for example.

Nick Meyer
+11  A: 

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 the ostream class, of which cout is an instance. The class definition sits in the iostream library, which is #include'd as <iostream>.

Carl Smotricz
I think it's a great tool to say exactly what you want in your code, and very useful when using stringstreams and fstreams for piping data into something
rubenvb
Actually, if you want to be pedantic, `operator<<` is a free function with an `ostream` as it's first argument.
Billy ONeal
@Billy ONeal: Surely you're more of an expert than I am, but isn't it an instance method of the `ostream` class, with (whatever's to the right) as its first argument?
Carl Smotricz
I don't think operator overloading should be used at all because class methods can achieve the same functionality. In the case of `ostream`, the `operator<<` functionality can be achieved by having a method such as `write_formatted` that is chainable.
Exception
@Exception: Thank you for your opinion. Unfortunately what you're looking for breaks down when someone tries to do something like `myOstream << myClass`. Someone can add `operator<<` s just by adding an additional method; however one cannot just stick methods onto standard library classes.
Billy ONeal
@Carl: Actually, we're both right -- several of the operator<<s on primitive types are member functions. However the overloads for `const char *` are free functions. Also, any user defined class that is shifted out will be written in terms of a free function.
Billy ONeal
@Exception: I also don't think that the stream insertion operator should have an overloaded meaning for integral types, when the same functionality can be achieved by adding a library function `lshift`.
avakar
"Operator overloading as a technique has been widely criticized in the programming language community". Although the biggest direct criticism I've seen of operator overloading in C++ came with the design of Java, which itself (hypocritically, C++ fans could argue) uses operator overloading, for example in String addition. It just doesn't allow mere programmers to overload operators - that right is reserved to Language Designers ;-) (In reality, the biggest direct criticism of C++ operator overloading might be the average programmer's use of it...)
Steve Jessop
+4  A: 

Like any operators in c++, << is doing operations. Using overloading, with an ostream left operand (std::cout is of ostream type), it's used as a stream operator to print data of various types. For example, you can do

int x = 10;
std::string y = " something";
std::cout << x << y << std::endl;

This will output "10 something".

@ is not replaced by anything in this context. operator<< just dump the result.

std::endl is not only the end of line, it also flushes the result to the output device.

Scharron
Ross
+2  A: 

Try writing a program where you create an object and call the overloaded << operator,

class x {
    //declare some pvt variables
    //overload << operator
};

int main() {
    x obj;
    cout << obj;
}

By doing so you will understand the rationale behind using the following statement

cout << string_var << int_var;

You can assume 'string' and 'int' as classes that have overloaded << operator even though not true.

Prabhu Jayaraman