<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 28 Scope and Lifespan_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 28: Scope and Lifespan
 More of this Feature
• Introduction, Local Scope
• Example 1
• Example 2
• Example 3
• Example 4
• Example 5
• Lifetime
• Global Scope
• Multilple Files
• Header Files
• Common Bug
 
 Related Resources
• Lesson 2: Variables
• Lesson 15: Dynamic Memory Allocation
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• C++ Tutorial - See all lessons
 

by John Kopp

Welcome to About.com's tutorial on C++ programming. This lesson covers the topics of variable scope and lifetime. Every object in a program has an identifier or name. Many complex questions arise from this seemingly simple statement. Does each identifier need to be unique? Where is an identifier valid? Where can it be used? Where can it not? The answers to these questions are found by understanding the concept of scope.

A variable's scope is the region of code in which the compiler can uniquely resolve its identifier. Within a particular scope, an identifier must be unique. In C++, objects can have local, global (namespace) or class scope. A variable with local scope is visible only within the function or code block in which it is defined. Variables with global scope are visible and can be used through out a program. Variables have global scope (or namespace scope) if they are defined in the part of the code that is outside of any class or function definition. A class definition also delimits a block of code and a scope.

Closely related to scope is the topic of lifespan or lifetime. How long does a variable exist? Will it exists for the entire duration of a program or only for the duration of a function or method call? As we will see, an object's lifetime depends largely on its scope.

Local Scope
Functions or class methods define a local scope. Code blocks delimited by brackets, {}, and certain statements also define local scopes. Variables within a particular local scope can only be accessed by code within that particular code block. Outside of the block, they do not exist. Each identifier must be unique within a scope. The compiler must resolve each identifier within a scope to a particular memory location. The definition of an object allocates storage space for the object in memory. During compilation, each name (identifier) must be resolved or linked to a particular physical location in memory. This means that every definition in a local scope must use a distinct identifier.

To understand all of this, let's look at a few examples.



Next page > Example 1 > Page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.