Upload
lewis-swanson
View
25
Download
3
Embed Size (px)
DESCRIPTION
CS240 Computer Science II. “Scope”. Two principle forms of scope. Local: scope of a block. File scope: has names that are external or global. Storage (classes) types. Automatic: auto (default class) Resister: register ( a request to compiler) - PowerPoint PPT Presentation
Citation preview
CS240 Computer Science II
“Scope”
Two principle forms of scope
• Local: scope of a block.
• File scope: has names that are external or global
Storage (classes) types
• Automatic: auto (default class)
• Resister: register ( a request to compiler)
• External: extern (a mechanism to transmit information across modules)
• Static: static (retain its previous value when block is re-entered)
External storage class
• When a variable is declared outside a function, storage is permanently (life of a program) assigned to it, and its storage class is external (extern) by default.
• If a variable is external (global), it is visible to all functions after it. It can not be auto or register class, but can be static.
• The word extern (or implied) tells the compiler to look for it elsewhere, either in this file or in some other file. Thus two modules can be compiled separately.
• Information can be passed into a function by using external variables (global) or through the parameter mechanism (preferred).
Example of extern declarations
// file circle.cppconst double pi = 3.14159; // because of const, local to the file circle.cppDouble circle(double, radius){
return (pi * radius * radius);}
// file main.cpp#include <iostream.h>double circle(double); // function is automatically externint main( ){
double x;…cout << circle(x) << “is area of circle of radius “ << x;return 0;
}
Class scope rules
Scope resolution operator :: is the highest precedence operator in C++; it has two forms::: i; // unary; refer to external scopeclassname :: i;// binary; refer to class scope
The unary operator is used to access a name that is at external scope and has been hidden by local or class scope, as shown in the next slide.
External linkage
A program can be thought of as a collection of separate modules or compilation units linked together. When a function in one module is called from another module, the function must have external linkage. By default, names of functions and variables are external. The calling module must be given information about the function name and parameter types (function prototype).
External linkage example
• In module 1:int sum(int a, int b);{
return a+b;}
• In module 2:…int sum(int, int);…Int n = sum(5, 2);…
Internal linkage
• The word static assigns internal linkage to a name, thus making it hidden inside a module.
• A function with internal linkage cannot be called directly by name from another module, as seen in the following example.
Internal linkage example
• Module 1
…
static int sum(int a, int b){ return a + b;}
…• Module 2:
…float sum = 0.0; // unrelated to sum function in module 1
…
Example: unary scope resolution operator
int count = 0; // external or global variable
// if not initialized explicitly,
// system will initialize it to 0.
void f(double w[ ], double x, int &count)
{
for (int j = 0; j < N; ++j)
count += (w[j] == x); // local count
++ ::count; // global count
}
Assignments
• Build test cases to verify all the discussions.