tags:

views:

160

answers:

3
namespace std  
{ 
  extern istream cin;   
...
}

By using extern we declare that cin is defined in some other unit as the answer

But what if istream is defined/undefined in std,there should be some difference,right?

What is the difference for the compilor?

+2  A: 

The compiler does not care. The linker will fail to "link" the externed object to the real object if it is undefined.

Goz
Do you mean the linker takes `istream cin` as a whole instead of as two parts:`istream` and `cin`?
symfony
No, the compiler checks that `istream` is a type and assumes that `cin` identifies an object of that type that will later be defined/linked
David Rodríguez - dribeas
So `istream` must be defined at compile time,while `cin` can be later defined runtime by the linker?
symfony
Not run time, at link time. It is defined "somewhere else". The linker just links the definition to the definition that is elsewhere. It gets a bit more complicated when you start talking about DLLs.
Goz
Let me re-conclude:`istream` must be defined at compile time,while `cin` can be later defined at link time by the linker.Is it right this time?
symfony
@symfony: Please read up a little on some fundamental C++ concepts, such as variable declarations vs. variable definitions, class definitions vs. instantiations, and the concept of separate compilation. Any beginner C++ book should introduce you to these concepts. Then come back here and ask specific questions about what you do not understand. Do you have a reason to understand these things? It helps to.
+1  A: 

The compiler would normally give an error finding that you're using cin without having, at least, declaring it.

With extern, you can tell the compiler "easy, easy, trust me, there is somewhere else a declaration and a definition for a cin of class istream.

Then the linker jumps into action, and the link between the call to the uses of cin and the object itself are specially "pending". The linker has to unite all these calls with their destination, and now is when the fact of cin existing or not (has been compiled or not) has its importance. If not, then the linker fails. The errors provided by the linker are far more cryptic than the ones given by the compiler, but they are interesting to explore, because are a very good way to learn.

For example, the following piece of code does not #include cstdio not stdio.h, but we know that printf will be there, because the standard library is always linked with our program. Yes, it works.

extern int printf(const char *, ...);

int main()
{
    printf( "Hello, world!\n" );
}
Baltasarq
A: 

The compiler must know that istream is a type, and with the extern keyword you are telling it that std::cin exists and is of that type. if istream has not been declared as a type by the time the compiler sees the line, it will complain telling you that it is not a type.

//extern type var; // error 'type' unknown at this point
class type;
extern type var;   // ok:'type' is a class even if not fully declared
David Rodríguez - dribeas
So the type must be defined when reaching that line,but the object can be checked by linker later?
symfony
The type must be at the very least forward declared. The line is read by the compiler as: 'I am telling you that `var` is a variable that will exist (and be of type `type`) so that you know when you see `var`', but the compiler will verify that `type` is actually a type. Else it will complain 'Wait a minute, but I don't know whether `type` is a type!!'. That check will detect errors if the actual type is `Type` and you just misspelled it.
David Rodríguez - dribeas
So only variables can be later defined?
symfony
Declaration and definition are two different things. `class name;` is the declaration of a class of name `name`, but the class can be defined before or after that line. If the class is defined *after* the declaration is found, the declaration is called a *forward declaration*. Types and variables are whole different entities in the language, and it seems as if you are trying to apply the same rules to both of them. I do believe that you should get a good book or tutorial to start with.
David Rodríguez - dribeas