This is from the <iostream>
:
namespace std
{
extern istream cin; ///< Linked to standard input
extern ostream cout;
...
It seems by using extern
the data types defined in other namespaces will just be available?
This is from the <iostream>
:
namespace std
{
extern istream cin; ///< Linked to standard input
extern ostream cout;
...
It seems by using extern
the data types defined in other namespaces will just be available?
No, this is an explicit way to say cin
and cout
are declared without actually defining them.
extern
means "these variables are defined in some other compilation unit (.cpp or .lib file)"
In this case, you #include <iostream>
into your .cpp file, and because cin
and cout
are declared as extern
, the compiler will let you use them without complaining. Then, when the linker runs, it looks up all of the extern
variables and sorts it all out.
extern
is used to refer to a variable defined in a different compilation unit (for now, you can think of a compilation unit as a .cpp file). The statements in your example declare rather than define cin
and cout
. It is telling the compiler that the definition of these objects is found in another compilation unit (where they are not declared as extern
).