I come from a C# and Java background into C++ and I\'m trying to get to understand the >>
& <<
operators such as in
std
<<
is the left-shift operator, and >>
is the right-shift operator, just as they are in Java and C#.
However, additionally, <<
is overloaded to provide a way of outputting values to a stream. The stream std::cout
usually refers to the terminal the program was launched in, and writing something to that stream with <<
will write it to the terminal. >>
is similarly overloaded to read from streams, and in the case of the stream std::cin
to read from the terminal the program was launched on.
This kind of thing works in C++ because you define the behaviour of operators for user-defined types. In java, operators operate only on built-in types - writing a + b
is an error if a
and b
are instances of your own class. In C++, this is allowed if the class defines a suitable operator +
.
As far as I remember, c++ has streams
<< is an operator that is an operator that is used to "append data" to a stream
And >> is used to extract data from a stream.
Of course, you can implement this with classes which are not just streams.
You didn't spell it out exactly, but I believe that your confusion is that you think that std::cout
is a function, and you're wondering why you don't just call it like this:
std::cout("Hello World");
Well, std::cout
is not a function. The function in this statement is operator<<
.
std::cout << "Hello World";
Or, more specifically, the function is std::ostream::operator<<(const char*)
.
The thing you need to understand is that operators are just functions with an alternative calling syntax. operator<<
is overloaded as a member function of std::ostream
, and std::cout
is an object of std::ostream
. So this:
std::cout << "Hello World";
Is an alternative way to call this:
std::cout.operator<<("Hello World");
Note that operator<<
is a binary operator, which means it takes two arguments, if it's declared as a free function, and one argument if it is declared as a member function. When you use the alternative calling syntax, the object on the left is the first argument, and the object on the right is the second argument. In the case where it is declared as a member function, as it is in this case, the object on the left is the calling object, and the object on the right is the argument.
Here's what it would look like if it were declared as a free function:
operator<<(std::cout, "Hello World");
But, whether it is declared as a member or a free function, you can still use the same alternative calling syntax.
In the case of I/O streams the <<
and >>
operators are overloaded to mean something other than bit shifting. For example, std::ostream::operator<< is overloaded for many types, and you can add overloads for your own types as well. For example:
#include <string>
#include <iostream>
class my_type {
public:
my_type(const std::string &name) : _name(name) { }
const std::string &get_name() const { return _name; }
private:
std::string _name;
};
std::ostream& operator<< (std::ostream& out, const my_type &foo ) {
return out << foo.get_name();
}
int main() {
my_type m("foo");
std::cout << m; // prints "foo"
return 0;
}
In C and C++ the << operator means "shift left". In C++, when using the standard template library, the << and >> operators are overloaded to implement a streaming API.
The object std::cout does overload the << operator to use it as a conversion function (in this case to a string printed to the console).
Actually this:
int x = myFunction();
std::cout << x;
and this:
std::cout << myFunction();
are functionally the same (besides the definition of the temporary variable x).
In general the <<
and >>
operators have the same meaning. However, in the cases that you have given they are stream write and read operators respectively. Specifically, <<
is the write operator (think of it as pointing towards the stream) and >>
is the read operator (pointing away from the stream).
They are handled by overloading the <<
and >>
operators. In particular, the C++ paradigm is to add overloads for <<
and >>
for ostream
s and istream
s respectively to handle input and output. C++ does not have a built-in paradigm of ToString()
the way Java and C# do.
Finally, std::cout
, std::cin
and std::cerr
are instances of ostream
, istream
and ostream
respectively, which is why they get the <<
and >>
overloads.
For instance, the following code outputs Hello World
to the standard output stream.
std::cout << "Hello World" << std::endl;
Finally, std::endl is a special ostream
that adds a newline and flush the stream.