Lecture Notes for Introduction to Computer Science II

10 July 2001 - Scope Nesting and Scope Resolution


  1. scope nesting

    1. blocks in statements in functions in classes in files

    2. inner and outer scopes

    3. inner scope variables hide outer scope variables - name-space collisions

    4. heavy use of nested scope access is usually considered bad programming

      1. difficult to understand - information is smeared around

      2. tricky to modify - larger scope means more dependencies

      3. but sometimes necessary and useful

  2. scope resolution

    1. how do i access

      1. a name hidden in an outer scope - int i ; int f(void) { int i; . . .}

      2. a name in a separate scope - class components

    2. the scope resolution operator ::

      1. scope-name :: name

      2. scope-name is either empty or a class name

        1. ::name - access name at file (global) scope; an indicator of poor design

        2. applicable to other scopes too - name spaces

  3. class definition and implementation

    1. why separate definition and implementation

      1. stability - changes in implementation don't change the definition; no excessive recompilations

      2. size - definitions are smaller than implementation; faster compilations

      3. information hiding - hiding the implementation makes it easier to change

    2. how to tie the implementation of f() with its class definition

    3. use the scope operator to indicate f() is part of C - C::f()

    4. public-private doesn't apply here

    5. other class members may be accessed similarly, but not instance member variables

    6. don't confuse . access and :: access to class members

      1. . access to instance members

      2. :: access to class members


This page last modified on 9 July 2001.