Object-Oriented Programming

Scope

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Variable

A variable is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value

  • storage location is different than symbolic name
  • name binding Associating a symbolic name with a storage location

Scope

The scope of a name binding is the portion of source code in which a binding of a name with an entity applies

  • Where in the program the name can be used to refer to the entity
  • In other parts of the program, the name may refer to another location, i.e., it is not the same variable
  • Scope is the visibility of the name binding
  • lexical scope or static scope portion of the source code, i.e., the area of text

Scope Levels

  • Global Scope
  • Module/Class Scope
  • File Scope
  • Function Scope
  • Block Scope
  • Expression Scope

Expression Scope

  • Name binding limited to the expression
  • Most common occurrence is in function declarations or function prototypes, known as function protocol scope
  • In a function declaration, the scope of parameter names ends right before the semicolon
  • Parameter names must be unique only in the parameter list
  • Parameter names are a form of documentation
  • Maybe more expressive in function declaration than in function definition

Block Scope

  • Name binding is limited to the block statement of the declaration
  • An automatic variable or local variable is allocated where it is declared and deallocated automatically when the program leaves the variable's scope
  • visibility of a variable may be hidden by shadowing

Function Scope

  • Name binding is limited to the block of the function
  • Similar to block scope but for functions
  • How local variables for a function are created

File Scope

  • Name binding is available to the file of the declaration
  • Available where declared until the end of the file
  • Created when the program starts and destroyed when it ends
  • In essence, global to the file
  • Order of construction is the order of declaration
  • Most use is for constants needed in the rest of the file
  • Use of the specifier static or and anonymous namespace
  • Mostly seen in C/C++

Proper File Scope Variables

  • File scope variables may be accessible by other files in the program
  • To ensure scope is limited to the file with the declaration, use either the specifier static or an anonymous namespace
  • Prefer an anonymous namespace

C++ Function Naming Tools

Description Command
Compile to Assembly g++ -S overloading.cpp
Compile to x86 Assembly on M1 Mac g++ -target x86_64 -S overloading.cpp
List symbols in object file nm overloading
Demangle a symbol c++filt __Z6actioni
Demangle all symbols in object file nm --demangle overloading

nm Symbol Types

Symbol Definition Description
U Undefined External storage, found during linkage
D Data section Stored locally, available globally
d Data section local Stored locally, available locally only
T Text (code) section  
S Another section Uninitialized or zero-initialized data section for small objects

 

Module/Class Scope

  • Name binding is limited to the class definition and method definition
  • In non-OOP languages, this is accomplished by modules
  • In the class definition, names must be declared before they are used
  • method definitions have access to all of the fields (data members) of their class, i.e., the fields are in scope

Global Scope

  • Name binding is available to more than one source-code file of a program
  • Called global variables, as with File Scope
  • File with storage location declares as normal
  • File using it externally uses the extern specifier
  • Often, the entire program has access to the variable
  • Created when the program starts and destroyed when it ends

Global Scope

Global Scope Problems

  • All global variables are created before entering the main() function
  • However, the order of creation between different translation units is not specified by the C++ Standard
  • This code segfaults when run
  • Known as the static initialization order fiasco

Scope & Design

A good way to improve a design is to minimize scope whenever possible

  • Choose a local variable over a parameter or a field
  • Place the variable in the innermost block possible
  • If possible, declare local variables where they first receive a value
  • Replace all global variables with class members
  • Reduce the amount of code that has access to a variable