为什么在函数内部对extern变量进行初始化会产生错误? [英] Why does initializing of an extern variable locally inside a function give an error?

查看:3467
本文介绍了为什么在函数内部对extern变量进行初始化会产生错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码编译得很好:

  extern int i = 10; 

void test()
{
std :: cout< Hi< i<< std :: endl;
}

这个错误提示:

  void test()
{
extern int i = 10;
std :: cout<< Hi< i<< std :: endl;
}




错误:'我'既有'的extern 和初始化




我读的 C ++入门




包含显式初始化程序的任何声明都是定义。
我们可以提供上定义为外部变量的初始化,但
这样做将覆盖EXTERN。一个具有初始化程序的extern是一个
定义。 这是一个错误,提供一个
函数内部一个外部


的初始

灿有人提供了一个解释,为什么这应该是如果一个函数在本地完成,而同样被允许在全球范围内的错误?


解决方案

之所以定义一个函数无厘头内外部变量如下:



在将符号声明的extern,你告诉编译器将所有这些链接这个值出现在同一符号中。任何出现的extern int i;在你的程序中将链接到外部定义的i。看看这个例子:

  #include< iostream> 
using namespace std;

extern int i;
int i = 10;
void test()
{
std :: cout<< Hi< i<< std :: endl;
}

int main()
{
extern int i;
i ++;
test();
}

此示例应输出hi11。然而,如果我们删除extern里面main,它将输出10.这是因为没有extern,我不是链接到全局i,但创建它自己的本地副本i。



在函数中定义extern i没有意义的原因是,如果我们允许任何函数definei。哪个函数先运行?



假设下面的例子是有效的,输出是什么?

  #include< iostream> 
using namespace std;

extern int i;
int i = 10;
void test()
{
std :: cout<< Hi< i<< std :: endl;
}

void test2(){
extern int i = 1000;
std :: cout<< HI< i<< std :: endl;
}

void test3(){
extern int i;
i = 1000;
std :: cout<< HI< i<< std :: endl;
}

int main()
{
extern int i;
i ++;
test();
i = 0;
test2();
}

test2的输出是0还是1000?还看看我的test3,这里我们简洁地说,链接我的外部定义的i,并将其值分配为1000.这是非常不同的尝试初始化一个值。



简而言之,extern变量只作为全局变量才有意义,应该在全局范围内定义。在你的例子中,第一个版本不为我编译。我觉得这很有趣。它可能值得看看标准文档,看看这是否定义简明扼要,或者如果你的编译器可能正在处理这种方式旨在添加额外的保护...


This code compiles fine:

extern int i = 10;

void test()
{
    std::cout << "Hi" << i << std::endl;
}

While this one gives an error:

void test()
{
    extern int i = 10;
    std::cout << "Hi" << i << std::endl;
}

error: 'i' has both 'extern' and initializer

I read in C++ Primer

Any declaration that includes an explicit initializer is a definition. We can provide an initializer on a variable defined as extern, but doing so overrides the extern. An extern that has an initializer is a definition. It is an error to provide an initializer on an extern inside a function.

Can someone provide an explanation why this should be an error if done locally in a function, while the same is allowed at a global scope?

解决方案

The reason defining an external variable inside a function does not make sense is the following:

When you declare a symbol extern, you are telling the compiler to link all such occurrences of this value into the same symbol. Any occurences of extern int i; in your program would link to the externally defined i. Look at this example:

#include <iostream>
using namespace std;

extern int i;
int i = 10;
void test()
{
    std::cout << "Hi" << i << std::endl;
}

int main()
{
    extern int i;
    i++;
    test();
}

This example should output hi11. HOwever, if we remove the extern inside main, it will output 10. This is because without extern, i is not linking to the global i, but creating it's own local copy of i.

The reason that defining an extern i inside a function does not make sense, is what if we allowed any function to "define" i. Which function runs first? When does it get defined?

Assume the following example to be valid, what would the output be???

#include <iostream>
using namespace std;

extern int i;
int i = 10;
void test()
{
    std::cout << "Hi" << i << std::endl;
}

void test2() {
    extern int i = 1000;
    std::cout<< "HI" << i << std::endl;
}

void test3() {
    extern int i;
    i = 1000;
    std::cout<< "HI" << i << std::endl;
}

int main()
{
    extern int i;
    i++;
    test();
    i = 0;
    test2();
}

Should the output of test2 be 0, or 1000? Also look at my test3, here we are concisely saying, link my i to the externally defined i, and assign it's value as 1000. This is very different from trying to "initialize" a value.

In short, extern variables really only make sense as globals, and should be defined in global scope. In your examples, the first version doesn't compile either for me. I find this interesting. It might be worth looking at the standards docs to see if this is defined concisely, or if your compiler might be handling this in a way designed to add additional protection...

这篇关于为什么在函数内部对extern变量进行初始化会产生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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