Scope J. Michael Moore
Scope Scope: Region of code “In Scope” Name declared in a scope is valid until the end of the scope
Types of Scope Global scope: the area of text outside of any other scope Avoid for variables OK for constants Namespace scope: named scope nested in the global scope or in another namespace Class scope: area of text within a class Local scope: between {…} braces of a block or in a function argument list Can be nested Statement Scope: In a for-statement
For some Examples: https://en.wikibooks.org/wiki/C%2B%2B_Programming/Scope/Examples
Global Variables Normally considered “evil” Toyota Accelerator Bug “Other egregious deviations from standard practice were the number of global variables in the system. (A variable is a location in memory that has a number in it. A global variable is any piece of software anywhere in the system can get to that number and read it or write it.) The academic standard is zero. Toyota had more than 10,000 global variables. “And in practice, five, ten, okay, fine. 10,000, no, we're done. It is not safe, and I don't need to see all 10,000 global variables to know that that is a problem,” Koopman testified.” Taken from Toyota Unintended Acceleration and the Big Bowl of “Spaghetti” Code Nothing we do in this course needs more than the academic standard, i.e. zero!