20.5.  Review Questions

[ fromfile: scopestorage-questions.xml id: questions-scopestorage ]

  1. What is a scope? What kinds of "things" have a scope?

      Scope is a region of code where an identifier can be used. All identifiers have scope.

  2. What is a storage class? What kinds of "things" have a storage class?

      Storage class is an area of memory. objects, temporary expresisons, variables, all have a storage class.

  3. When are static objects initialized? Be sure to consider both globals and block scope objects.

      For globals, when the object module (library or executable) is loaded. For block-scope locals, they are initialized when the code is executed for the first time.

  4. How does const act as a scope modifier?

      It turns global scope objects into file-scope objects (not exported by the linker). They are local to the source module only.

  5. What does extern mean?

      adding the keyword extern turns a definition into an external declaration. No storage is allocated, no initial value can be assigned. The object must be defined in another module. extern tells the compiler let the linker figure out where the definition is.

  6. The keyword static has many meanings depending on where it is used.

    1. Explain how static can be used as a scope modifier.

        It changes a global name to file-scope, preventing it from being exported to the linker.

    2. Explain how static can be used as a storage-class modifier.

        When applied to a block scope object, it changes the storage class from stack to static.

    3. Give another use for the keyword static.

  7. What is the storage class of an object defined in a namespace?

  8. What is special about a pointer to an object declared using the keyword register?

  9. What is the difference between a class and a namespace?

  10. What must you do if you want to declare an object in the header file of a namespace without defining it?