Use a global variable : global variable « Language Basics « C++ Tutorial






#include <iostream>
using namespace std;

void func1();
void func2();

int count; // This is a global variable.

int main()
{
  int i; // This is a local variable

  for(i=0; i<10; i++) {
    ::count = i * 2;
    func1();
  }

  return 0;
}

void func1()
{
  cout << "count: " << ::count; // access global count
  cout << '\n';
  func2();
}

void func2()
{
  int count;

  for(count=0; count<3; count++)
     cout << '.';
}
count: 0
...count: 2
...count: 4
...count: 6
...count: 8
...count: 10
...count: 12
...count: 14
...count: 16
...count: 18
...








1.5.global variable
1.5.1.Use a global variable
1.5.2.Global class variable
1.5.3.Global variables are known throughout the entire program and may be used by any piece of code.
1.5.4.Use :: to reference global variable