【问题标题】:Static And Global Variables In Different Scopes不同作用域的静态变量和全局变量
【发布时间】:2020-02-15 20:09:09
【问题描述】:

我是一个初学者程序员...这里我有一段代码有两个函数...其中一个使用全局变量,而另一个使用局部静态变量...

代码如下:

//Scope Rules
#include <iostream>
using namespace std;

int num {52};     //Global Variable - Declared outside any class or function

void globalExample();
void staticLocalExample();

void globalExample(){
    cout << "\nGlobal num in globalExample is: " << num << " - start" << endl;
    num *= 4;
    cout << "Global num in globalExample is: " << num << " - end" << endl;
}
void staticLocalExample(){
    static int num {5};      //local to staticLocalExample - retains it's value between calls
    cout << "\nLocal static num in staticLocalExample is: " << num << " - start" << endl;
    num += 20;
    cout << "Local static num in staticLocalExample is: " << num << " - end" << endl;
}

int main() {

    globalExample();
    globalExample();
    staticLocalExample();
    staticLocalExample();

    return 0;
}

我两次调用这些函数的原因是为了让我明白第二次调用该函数时,更改将存储到变量中,因此结果会有所不同。

我不明白的是:

  1. 那么这两个函数有什么区别呢?
  2. staticLocalExample 函数到底是做什么的?(因为比较起来,两者的结果是一样的)
  3. 我们为什么以及何时使用关键字static

注意1:我知道改变的原因是我在这两个函数中的分配但是......这只是一个例子,我并没有悄悄理解它

注意2:我检查了你们发给我的链接,非常感谢你们……我理解了它背后的概念,但我仍然找不到第三个问题的答案:)

【问题讨论】:

  • 相关question 如果不是完全重复的话。
  • 这能回答你的问题吗? On local and global static variables in C++
  • 全局可以在任何地方更改。静态局部变量只能由其作用域所在的函数更改。这有助于管理代码的复杂性,因为它会变得越来越大(相信我,代码库可能巨大
  • 真的没有什么其他的了。正如其他人所说,这个问题最好用一本好书来回答。一本书可以向您展示和解释为什么在全局命名空间中拥有很多东西是一个坏主意的几个原因
  • C++ 是当今使用的最复杂和最难的通用编程语言。 C++ 的正式技术规范大约有 2000 页。没有一本好书,你会发现很难学到很多东西。

标签: c++ c++11 c++14


【解决方案1】:

我想我得出了一个结论...所以如果我错了请告诉我...

全局变量:您可以更改它们并在其范围之外或范围内分配不同的数字

静态变量:你只能在它的范围内改变它,而不是在它之外

所以最后让我们稍微修改一下这段代码:

#include <iostream>
using namespace std;

int num {52};     //Global Variable - Declared outside any class or function

void globalExample();
void staticLocalExample(int x);

void globalExample(){
    cout << "\nGlobal num in globalExample is: " << num << " - start" << endl;
    num *= 4;
    cout << "Global num in globalExample is: " << num << " - end" << endl;
}
void staticLocalExample(int x){
    static int num {5};      //local to staticLocalExample - retains it's value between calls
    cout << "\nLocal static num in staticLocalExample is: " << num << " - start" << endl;
    num += 20;
    cout << "Local static num in staticLocalExample is: " << num << " - end" << endl;
}

int main() {

    globalExample();
    num = 20;
    globalExample();
    staticLocalExample(10);
    staticLocalExample(15);

    return 0;
}

输出:

Global num in globalExample is: 52 - start
Global num in globalExample is: 208 - end

Global num in globalExample is: 20 - start     <-------
Global num in globalExample is: 80 - end

Local static num in staticLocalExample is: 5 - start
Local static num in staticLocalExample is: 25 - end

Local static num in staticLocalExample is: 25 - start
Local static num in staticLocalExample is: 45 - end

如您所见,全局变量已更改,但静态变量没有... 我所要做的就是在代码块之外而不是在代码块内部思考......

谢谢Cassio Renan

【讨论】:

    猜你喜欢
    • 2022-01-28
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    相关资源
    最近更新 更多