extern如何在c ++中工作? [英] How does extern work in c++?

查看:210
本文介绍了extern如何在c ++中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自< iostream>

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

似乎使用 extern

It seems by using extern the data types defined in other namespaces will just be available?

推荐答案

extern 用于引用在不同编译单元中定义的变量(现在,您可以将编译单元视为.cpp文件)。您的示例中的语句 而不是 cin cout 。它告诉编译器,这些对象的定义是在另一个编译单元中找到的(它们声明为 extern )。

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).

这篇关于extern如何在c ++中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆