为什么需要外部"x"?用来? [英] Why the external "x" is used?

查看:83
本文介绍了为什么需要外部"x"?用来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我是C ++初学者,也是CodeProject的新手.因为英语不是我的母语,所以我的表达可能不是很好.这是令我困惑的代码:

Hi! I''m a C++ beginner and a new guy of CodeProject. Because English is not my native language, maybe my expression is not very good. Here is the code that puzzles me:

#include <iostream>
using namespace std;

int x = 5;

int func()
{
	int x = 4;
	{
		extern int x;
		return x;
	}
}

int main()
{
	cout<<func()<<endl;
}



由gcc编译,运行时它会显示"5".如果我丢掉"extern int x;",它将打印"4".如果我写:



Compiled by gcc , it prints a "5" when I run it. If I throw the "extern int x; " away it prints a "4". And if I write:

int func()
{
	int x = 4;
	extern int x;
	return x;
}



然后程序发生错误.

所以问题是:1为什么引用外部"x"? 2为什么我不能扔掉包围外部声明的花括号?请给我一些提示.



Then the program occurs an error.

So the question is: 1 Why the external "x" is referenced ? 2 Why I can''t throw away the braces that encloses the extern declaration? Please give me a few hints.

推荐答案

看看这个: extern在C中的用途是什么? [ ^ ].我认为它很好地回答了这两个问题.
Have a look at this: What is the use of extern in C?[^]. I think it answers both questions quite nicely.


您在这里混合了两个基本概念:可见性和extern声明.

可见性是指变量名的解析:当存在多个同名声明时,编译器将使用最里面的作用域之一.在您的第一个示例中,该行是extern int x;行.在您的第二个示例中,这两个声明都在同一范围内,,但是在这种情况下,后一个隐藏了前一个.这将导致错误

请注意,就可见性规则而言,此声明是否包含extern无关紧要:在任何情况下,此声明都会有效地隐藏上面的声明:int x = 4;

extern告诉编译器您引用的是代码库中(或可能在外部)其他地方声明的变量.可以在相同或不同的编译单元内.无论如何,该变量声明必须在全局名称空间中,因此函数func()中的局部定义将不能用作外部变量.因此,在您的第一个代码示例中,只有一个变量声明可用作外部变量,这就是第一个. 第二个示例不包含x的全局定义.因此系统*无法解析您的声明extern int x;所引用的内容,从而导致错误.

(*:我希望链接器在此处发出错误;不确定您为什么说该程序会这么做-还是您的意思是链接器?)

[edit]固定[/edit]
You''ve mixed two base concepts here: visibility and extern declarations.

Visibility refers to the resolution of variable names: when there are multiple declarations of the same name, the compiler will use the one of the innermost scope. In your first example that is the line extern int x; In your second example the two declarations are in the same scope, but in this case the later one hides the former ones. this causes the error

Note that it does not matter whether this declaration contains extern with respect to visibility rules: in any case this declaration effectively hides the declaration above: int x = 4;

extern tells the compiler that you are referring to a variable declared elsewhere in (or possibly outside) your codebase. That may be within the same, or a different compilation unit. In any case, that variable declaration must be in the global namespace, so a local definition within your function func() will not be usable as an extern. therefore there is only one variable declaration usable as an extern in your first code example, and that is the first one. The second example contains no global definition for x however. therefore the system* cannot resolve what your declaration extern int x; refers to, and that causes the error.

(*: I would expect the linker to issue an error here; not sure why you said the program does - or did you mean the linker?)

[edit]fixed[/edit]


您不能取消花括号的原因是与范围有关.命名变量是指先前在相同作用域或最接近外部作用域中声明的变量.您可以在不同的作用域中使用具有相同名称的变量(它们甚至不必具有相同的类型),在这种情况下,当前可见的变量(例如,在相同或最接近的外部作用域中声明)将其他变量隐藏起来.

简而言之,在这种情况下,extern关键字告诉编译器使用全局范围内声明的"x",而不是最接近的范围.
The reason you can''t do away with the curly braces is to do with scope. A named variable refers to one previously declared in the same scope, or closest outer scope. You can have variables with the same name in different scopes (they don''t even have to be the same type), in which case the one currently visible (eg declared in the same or closest outer scope) hides the others.

Put simply, the extern keyword in this case tells the compiler to use the "x" declared in the global scope, not the one closest.
int x = 5; // Global scope

int func()
{
  // At this point, "x" refers to the global, with value 5
  int x = 4;
  // A local "x" has been declared in this scope, so it hides the global
  {
    // New scope, closest declaration (x=4) is still the visible one
    extern int x; // Now "x" refers to the global
    return x;
  }
}



如果扔掉括号,则在同一作用域中有两个"x"(局部和全局),这是一个错误.



If you throw away the braces, you have two "x" in the same scope - both the local and the global - and that is an error.


这篇关于为什么需要外部"x"?用来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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