Lecture Notes for Introduction to Computer Science II
10 July 2001 - Scope Nesting and Scope Resolution
- scope nesting
- blocks in statements in functions in classes in files
- inner and outer scopes
- inner scope variables hide outer scope variables - name-space
collisions
- heavy use of nested scope access is usually considered bad programming
- difficult to understand - information is smeared around
- tricky to modify - larger scope means more dependencies
- but sometimes necessary and useful
- scope resolution
- how do i access
- a name hidden in an outer scope -
int i ; int f(void) { int i; . . .}
- a name in a separate scope - class components
- the scope resolution operator
::
- scope-name
::
name
- scope-name is either empty or a class name
-
::
name - access name at file (global) scope; an
indicator of poor design
- applicable to other scopes too - name spaces
- class definition and implementation
- why separate definition and implementation
- stability - changes in implementation don't change the definition; no
excessive recompilations
- size - definitions are smaller than implementation; faster
compilations
- information hiding - hiding the implementation makes it easier to
change
- how to tie the implementation of
f()
with its class definition
- use the scope operator to indicate
f()
is part of C
-
C::f()
-
public
-private
doesn't apply here
- other class members may be accessed similarly, but not instance member
variables
- don't confuse
.
access and ::
access to class members
-
.
access to instance members
-
::
access to class members
This page last modified on 9 July 2001.