1
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
  • Exact duplicate (same user): stackoverflow.com/questions/2513108 Commented Mar 25, 2010 at 10:01
  • I am not sure that the question is a exact duplicate. The first question dealt with the whole sentence, and it seems as if the user is not unclear in the specifics of whether only the var or the type are introduced in the sentence. Commented Mar 25, 2010 at 11:45

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

Do you mean the linker takes istream cin as a whole instead of as two parts:istream and cin?
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
So istream must be defined at compile time,while cin can be later defined runtime by the linker?
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.
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?
|
1

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" );
}

Comments

0

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

4 Comments

So the type must be defined when reaching that line,but the object can be checked by linker later?
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.
So only variables can be later defined?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.