Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 117: Scopes

Section 117.1: Global variables

To declare a single instance of a variable which is accessible in di erent source files, it is possible to make it in the global scope with keyword extern. This keyword says the compiler that somewhere in the code there is a definition for this variable, so it can be used everywhere and all write/read will be done in one place of memory.

// File my_globals.h:

#ifndef __MY_GLOBALS_H__ #define __MY_GLOBALS_H__

extern int circle_radius; // Promise to the compiler that circle_radius // will be defined somewhere

#endif

//File foo1.cpp: #include "my_globals.h"

int circle_radius = 123; // Defining the extern variable

//File main.cpp:

#include "my_globals.h" #include <iostream>

int main()

{

std::cout << "The radius is: " << circle_radius << "\n";' return 0;

}

Output:

The radius is: 123

Section 117.2: Simple block scope

The scope of a variable in a block { ... }, begins after declaration and ends at the end of the block. If there is nested block, the inner block can hide the scope of a variable which is declared in the outer block.

{

int x = 100;

//^

//Scope of `x` begins here

}// <- Scope of `x` ends here

If a nested block starts within an outer block, a new declared variable with the same name which is before in the

GoalKicker.com – C++ Notes for Professionals

585

outer class, hides the first one.

{

int x = 100;

{

int x = 200;

std::cout << x; // <- Output is 200

}

std::cout << x; // <- Output is 100

}

GoalKicker.com – C++ Notes for Professionals

586