Day12

继续看范例fig06_12.cpp下半段,一开始定义三个函式原型void useLocal(), void useStaticLocal(), void useGlobal(), 我稍微修改了一下main(),透过for回圈各自呼叫三次,可以发现useStaticLocal()与useGlobal()的变数x的值各累加了两次,但 useLocal()里x的值并没有改变。

  • Result
local x is 100
local static x is 200
global x is 300

local x is 100
local static x is 201
global x is 301

local x is 100
local static x is 202
global x is 302
  • fig06_12.cpp
#include <iostream>
using std::cout;
using std::endl;

void useLocal(); // function prototype
void useStaticLocal(); // function prototype
void useGlobal(); // function prototype

int x = 300; // global variable

int main()
{
   for (int i = 0; i <3; i++)
   {
      useLocal(); // useLocal has local x
      useStaticLocal(); // useStaticLocal has static local x
      useGlobal(); // global x also retains its value
      cout << endl;
   }

   return 0; // indicates successful termination
} // end main

// useLocal reinitializes local variable x during each call
void useLocal( void )
{
   int x = 100; // initialized each time useLocal is called
   
   cout << "\nlocal x is " << x ;
   x++;
} 

void useStaticLocal( void )
{
   static int x = 200; // initialized first time useStaticLocal is called 

   cout << "\nlocal static x is " << x ;
   x++; 
} 

void useGlobal( void )
{
   cout << "\nglobal x is " << x ;
   x++; 
} 


<<:  [Day 10] -『 GO语言学习笔记』- if 叙述的起始赋值

>>:  Proxmox VE 虚拟机管理操作 (一)

Eloquent ORM - 一对一关联

Eloquent 可以在 Model 之间建立关联查询,这样可以藉由这些关联快速查询出所需的资料。 ...

[Day30] Angular 的 Routing

终於来到第三十天了~~~~!不过其实本系列不会在今天结束,我们的前端 app 丑得要命,也还没布署到...

D23 - 走!去浏览器用 create & append 加餐

前言 恭喜 codepen 复活 \(^∀^)メ(^∀^)ノ 终於可以透过实作来学习如何选取元素、操...

【Day 09】Sorting:Bubble Sort 气泡排序法 ( 用 JavaScript 学演算法 )

气泡排序法是,从第一个元素开始,和相邻数字比大小,若有需要就交换位置。因此也可称为交换排序法。它的...

DAY11 资料前处理-资料不平衡处理方法

试想一下,如果有个模型号称有99%的准确率,那这个模型好不好呢?答案是不一定,在处理分类问题时,我们...