Beginner
minutes
Scope and Lifetime Terminology
Overview of scope and lifetime concepts in this chapter.
Last updated:
This reference provides an overview of scope and lifetime terminology you'll encounter in this chapter. Use it as a quick lookup guide.
Scope Concepts
| Term | Definition | Example |
|---|---|---|
| Scope | Region of code where an identifier is accessible | Block, function, file, global |
| Local scope | Scope within a block or function | Variables inside {} |
| Global scope | Scope accessible throughout the program | Variables outside all functions |
| Block scope | Scope limited to a compound statement | Variables in if, for, while blocks |
| Namespace scope | Scope within a namespace | namespace MyLib { int x; } |
| Nested scope | Scope inside another scope | Inner block inside outer block |
Variable Lifetime
| Term | Definition | Example |
|---|---|---|
| Lifetime | Duration a variable exists in memory | Creation to destruction |
| Automatic duration | Created/destroyed with scope entry/exit | Local variables |
| Static duration | Exists for entire program execution | Global and static variables |
| Dynamic duration | Manually controlled with new/delete | Heap-allocated objects |
Storage Duration
| Term | Definition | Example |
|---|---|---|
| Automatic storage | Variable on the stack, scope-based lifetime | int x{5}; in function |
| Static storage | Variable persists for entire program | static int count{0}; |
| Thread storage | Variable unique to each thread | thread_local int x; |
| Dynamic storage | Variable on the heap, manual lifetime | new int{5} |
Linkage
| Term | Definition | Example |
|---|---|---|
| Linkage | Whether identifier can be seen in other files | Internal vs external |
| No linkage | Only visible in current scope | Local variables |
| Internal linkage | Visible only in current translation unit | static global variables |
| External linkage | Visible across translation units | Non-static global variables |
| Translation unit | A .cpp file after preprocessing | Source file + included headers |
Linkage Keywords
| Term | Definition | Example |
|---|---|---|
| static | Internal linkage for globals, persistent for locals | static int count{0}; |
| extern | Declares external linkage variable | extern int globalVar; |
| inline | Allows multiple definitions across files | inline int globalVar{5}; |
| const | Internal linkage by default for globals | const int maxSize{100}; |
| constexpr | Internal linkage by default | constexpr int size{10}; |
Namespaces
| Term | Definition | Example |
|---|---|---|
| Namespace | Named scope for organizing identifiers | namespace MyLib { } |
| std namespace | Standard library namespace | std::cout, std::string |
| Nested namespace | Namespace inside another namespace | namespace A::B { } |
| Anonymous namespace | Unnamed namespace with internal linkage | namespace { int x; } |
| Inline namespace | Members visible in enclosing namespace | inline namespace V2 { } |
Namespace Access
| Term | Definition | Example |
|---|---|---|
| Scope resolution | Operator to access namespace members | std::cout |
| Using declaration | Bring specific name into scope | using std::cout; |
| Using directive | Bring all names into scope | using namespace std; |
| Qualified name | Name with namespace prefix | std::vector<int> |
| Unqualified name | Name without namespace prefix | cout after using declaration |
Special Variables
| Term | Definition | Example |
|---|---|---|
| Global variable | Variable with global scope | Defined outside functions |
| Local variable | Variable with block scope | Defined inside functions |
| Static local | Local variable with static duration | static int count{0}; |
| Global constant | Read-only global variable | const int maxSize{100}; |
Shadowing
| Term | Definition | Example |
|---|---|---|
| Variable shadowing | Inner variable hides outer with same name | Inner x shadows outer x |
| Name hiding | Another term for shadowing | Nested scope hides outer name |
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Scope and Lifetime Terminology - Quiz
Test your understanding of the lesson.
7 questions
10 minutes
60% to pass
Lesson Discussion
Share your thoughts and questions
💬
No comments yet. Be the first to share your thoughts!