Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
The scope of a variable
is the area where its name is valid. A variable declared inside a function
is only valid inside that function. Thus its scope is inside the function.
A scope is defined by accolades, as shown in the commented example below:
A variable 'without scope' is called a
global variable. Avoid using
global data [1-5]. When the compiler can
choose between a global and local
variable with the same name, the local will be used, as shown in the example below:
Keep scopes small [6] . Don't use the same name in both a scope and an enclosing scope [7].
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 10: 'Minimize global and shared data'.
- Herb Sutter, Andrei Alexandrescu . C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 18: 'Declare variables as locally as possible'.
- Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4. Chapter 1.8.2.a: 'Don't use global data (use members)'
- Jarrod Hollingworth , Bob Swart, Mark Cashman, Paul Gustavson. Sams C++ Builder 6 Developer's Guide. ISBN: 0-672-32480-6. Chapter 3: 'Avoid using global variables'
- Jesse Liberty . Sams teach yourself C++ in 24 hours. ISBN: 0-672-32224-2. Hour 5, paragraph 'Global variables': 'In C++, global variables are avoided because they can create very confusing code that is hard to maintain.'
- Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Item 4.10.1: 'Keep scopes small'.
- Bjarne Stroustrup . The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Item 4.10.2: 'Don't use the same name in both a scope and an enclosing scope'.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
