C++ Tutorial/Language Basics/block scope variable

Материал из C\C++ эксперт
Перейти к: навигация, поиск

global and block scope

#include <iostream.h>
int n=0;  //Global
main()
{
        int n = 1;
        {
               int n = 2 ;
               {
                        int n = 3;
                        cout << "In inner  n=" <<n<<endl;
                        cout << "Global    n=" << ::n <<endl;
               }
               cout << "In outter n=" <<n<<endl;
               cout << "Global    n=" <<::n<<endl;
        }
        cout << "In main() n=" << n<<endl;
        return 0 ;
}
In inner  n=3
Global    n=0
In outter n=2
Global    n=0
In main() n=1

global variables across functions

#include <iostream>
using namespace std;
int glob = 10;  // global variable
void access_global();
void hide_global();
void change_global();
int main()
{
    cout << "In main() glob is: " << glob << "\n\n";
    access_global();
    hide_global();
    cout << "In main() glob is: " << glob << "\n\n";
    change_global();
    cout << "In main() glob is: " << glob << "\n\n";
    return 0;
}
void access_global()
{
    cout << "In access_global() glob is: " << glob << "\n\n";
}
void hide_global()
{
    int glob = 0;  // hide global variable glob
    cout << "In hide_global() glob is: " << glob << "\n\n";
}
void change_global()
{
    glob = -10;  // change global variable glob
    cout << "In change_global() glob is: " << glob << "\n\n";
}

Inner block variable scope

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  int i; 
  int j; 
 
  i = 10; 
  j = 100; 
 
  if(j > 0) { 
    int i; // this i is separate from outer i 
     
    i = j / 2; 
    cout << "inner i: " << i << "\n"; 
  } 
 
  cout << "outer i: " << i << "\n"; 
 
  return 0; 
}
inner i: 50
outer i: 10

Names in inner scopes can hide names in outer scopes.

#include <iostream>
#include <ostream>
int main()
{
  for (int i = 0; i < 10; ++i)
  {
    int x = 2;
    if (x < i)
    {
      double x = 3.4;
      std::cout << x; 
    }
    std::cout << x;   
  }
  //std::cout << x;     // Error: no x declared in this scope
}
2223.423.423.423.423.423.423.42

scope code block

#include <iostream>
using namespace std;
void func();
int main()
{
    int var = 5;
    cout << "In main() var is: " << var << "\n\n";
    func();
    cout << "Back in main() var is: " << var << "\n\n";
    {
        cout << "In main() in a new scope var is: " << var << "\n\n";
        cout << "Creating new var in new scope.\n";
        int var = 10;
        cout << "In main() in a new scope var is: " << var << "\n\n";
    }
    cout << "At end of main() var is: " << var << "\n";
    return 0;
}
void func()
{
    int var = -5;  // local variable in func()
    cout << "In func() var is: " << var << "\n\n";
}

Using the scope resolution operator: "::"

#include <iostream>
using std::cout;
using std::endl;
int count1 = 100;
int main() {     
  int count1 = 10;
  int count3 = 50;
  cout << endl << "Value of outer count1 = " << count1;
  cout << endl << "Value of global count1 = " << ::count1;
  {              
    int count1 = 20;
    int count2 = 30;
    cout << endl << "Value of inner count1 = " << count1;
    cout << endl << "Value of global count1 = " << ::count1;
    count3 += count2;
  }                 
  cout << endl
       << "Value of outer count1 = " << count1
       << endl
       << "Value of outer count3 = " << count3;
  cout << endl;
  return 0;
}
Value of outer count1 = 10
Value of global count1 = 100
Value of inner count1 = 20
Value of global count1 = 100
Value of outer count1 = 10
Value of outer count3 = 80

Using the scope resolution operator (2)

#include <iostream>
#include <ostream>
namespace n {
  struct counter {
    static int n;
  };
  double n = 2.8;
}
int n::counter::n = 7; 
int main()
{
  int counter = 0;    
  int n = 10;         
  std::cout << n::counter::n; 
  std::cout << n::n;          
  std::cout << x.n;           
  std::cout << n;             
  std::cout << counter;       
}
Value of outer count1 = 10
Value of global count1 = 100
Value of inner count1 = 20
Value of global count1 = 100
Value of outer count1 = 10
Value of outer count3 = 80

Variable block scope

#include <iostream>
using std::cout;
using std::endl;
int main() {
  int count1 = 10;
  int count3 = 50;
  cout << endl << "Value of outer count1 = " << count1;
  {
    int count1 = 20;
    int count2 = 30;
    cout << endl << "Value of inner count1 = " << count1;
    count1 += 3;
    count3 += count2;
  }
  cout << endl
       << "Value of outer count1 = " << count1
       << endl
       << "Value of outer count3 = " << count3;
  cout << endl;
  return 0;
}
Value of outer count1 = 10
Value of inner count1 = 20
Value of outer count1 = 10
Value of outer count3 = 80

Variables can be local to a block

#include <iostream> 
using namespace std; 
 
int main() { 
  int x = 19; // x is known to all code. 
 
  if(x == 19) { 
    int y = 20; 
 
    cout << "x + y is " << x + y << "\n"; 
  } 
 
  // y not known here. 
 
  return 0; 
}
x + y is 39