Ross
Ross

Reputation: 14415

What is the << operator doing in C++?

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

Upvotes: 6

Views: 1607

Answers (6)

josh
josh

Reputation: 14383

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.

Upvotes: 1

Carl Smotricz
Carl Smotricz

Reputation: 67750

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>.

Upvotes: 12

krb
krb

Reputation: 16315

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.

Upvotes: -4

Scharron
Scharron

Reputation: 17757

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.

Upvotes: 3

Nick Meyer
Nick Meyer

Reputation: 40272

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.

Upvotes: 3

Siqi Lin
Siqi Lin

Reputation: 1257

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

Upvotes: 3

Related Questions