Can someone please build up and elaborate on that answer.A few other things I would like to know are why can we include ostream if cout is of type OStream that is inside iostream? And if ostream is useless then why isn't it protetected?Does it serve a purpose besides being used in iostream? |
The
type std::ostream
is defined in the header file
<ostream>
:
https://en.cppreference.com/w/cpp/header/ostream
Meanwhile, the
global variables std::cout
and
std::cerr
are provided by the header file
<iostream>
:
https://en.cppreference.com/w/cpp/header/iostream
Even though the
global variables std::cout
and
std::cerr
are of the
type std::ostream
, it is perfectly possible to use the
type std::ostream
without using the
global variables std::cout
and
std::cerr
– and, in that case, you'd just include the
<ostream>
header. But, if you want to use the
global variables std::cout
and
std::cerr
, then including
<iostream>
is what you want 😏
In other words, the
type std::ostream
, which is defined in the
<ostream>
header, is
not "useless" without the
<iostream>
header, because that
type can very well be used for purposes that are
not related to
std::cout
or
std::cerr
at all! Only if you actually want to use
std::cout
or
std::cerr
, then you'll have to include the
<iostream>
header, because that's where those
global variables are declared.
So, yeah,
std::ostream
is somewhat realted to
std::cout
(and
std::cerr
), but they obviously are
not the same thing. The former may be used
alone without using the latter at all... And that is probably the reason why they are declared in
separate header files!
(Note that
<iostream>
implicitly includes
<ostream>
, but this does
not apply the other way around!)
______________________________
Consider the following example program:
main.cpp
1 2 3 4 5 6 7 8
|
#include <iostream>
#include <string>
void write_string(std::ostream &dst, const std::string &src);
int main() {
write_string(std::cout, "Hello World!");
}
|
utils.cpp
1 2 3 4 5 6
|
#include <ostream>
#include <string>
void write_string(std::ostream &dst, const std::string &src) {
dst << '"' << src << "\"\n";
}
|
Note how "utils.cpp" does
not know anything about
std::cout
or
std::cerr
, but rather accepts a reference to an
arbitrary std::ostream
instance to which it is going to write, and therefore it does
not need to include the
<iostream>
header 😏