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

查看:599
本文介绍了为什么在函数内部初始化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;
}

错误:"i"同时具有"extern"和初始化程序

我在 C ++ Primer 中阅读了以下内容:

任何包含显式初始化程序的声明都是定义. 我们可以在定义为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.但是,如果我们删除main内部的extern,它将输出10.这是因为没有extern,i不会链接到全局i,而是创建它自己的i本地副本.

在函数内部定义extern i的意义不大,这是如果我们允许任何函数定义" i的原因.哪个功能先运行?何时定义?

假设以下示例有效,输出将是什么?

#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链接到外部定义的i,并将其值分配为1000.这与尝试初始化"值非常不同.

简而言之,外部变量实际上仅对全局变量有意义,应在全局范围内定义.在您的示例中,第一个版本也不适合我编译.我觉得这很有趣.可能值得看一下标准文档,以查看是否对此定义进行了简洁的定义,或者您的编译器是否正在以旨在增加附加保护的方式来处理此问题...

This code compiles fine:

extern int i = 10;

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

While this code gives an error:

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

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

I read this 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 as to why this is 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天全站免登陆