How does extern work in c++?

后端 未结 5 1923
温柔的废话
温柔的废话 2021-01-12 10:03

This is from the :

namespace std 
{
  extern istream cin;       ///< Linked to standard input
  extern ostream cout;  
...
         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 10:52

    extern means "these variables are defined in some other compilation unit (.cpp or .lib file)"

    In this case, you #include 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.

提交回复
热议问题