zoukankan      html  css  js  c++  java
  • C++ 函数内静态静态变量

    #include <iostream>
    using namespace std;
    int& GetStaticVar() {
        static int a = 100;
        a++;
        return a;
    }
    int main(int argc, char const* argv[]) {
        cout << GetStaticVar() << endl;  // output:101
        int c = GetStaticVar();
        cout << c << endl;  // output:102
        c = c + 200;
        cout << c << endl;
        // 虽然函数返回的是局部静态变量的引用,上一步骤也对该变量进行了改变,
        // 但是实际上,在再次调用该函数的时候,该引用变量的值,并没有受到外部对该变量的修改而改变
        cout << GetStaticVar() << endl;  // output:103
    }
     
    结论:函数内静态变量系统会自动赋初始值,并且只能在该函数内进行修改
  • 相关阅读:
    perimeter of squares
    map
    django路由
    for的骚用法
    3和5的倍数相加和
    PeteCake 字典和最小值
    Find the missing letter
    实现简单的ssh功能
    开源运维工具体系
    vsftp在iptables中的配置
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/15519835.html
Copyright © 2011-2022 走看看