Scope

Every name in a C++ program must refer to a unique entity (an object, function, type or template). a name can be reused to refer to different entities provided that there is some context by which the different meanings of the name can be distinguished. The general context used to distinguish the meanings of names is scope.

C++ supports three forms of scope:
  1. local scope - within function definition (or function block) or compound statement (or block).
  2. namespace scope
  3. class scope

In C++, the scope of a variable ends with first encountered closing braces }. Declare variables close to the code where they are used and where they can be initialized is particularly useful in large procedures with many local variables. This improves code readability and helps avoid type-mismatch errors.

Index