Courses/CS 2124/Lab Manual/Arrays and C-Style Strings/Homework

From A-State Computer Science Wiki
Jump to: navigation, search

Wrapping Arrays

In English, the word “wrap” means “to cover or enclose”. Programmers often use the term wrap to mean the act of placing one kind of programming construct inside another. This is often done for convenience, compatibility, or to add features to an existing object. You will sometimes here the term “thin wrapper” used in object-oriented programming to mean that a minimal class has been constructed to “wrap” some object. If the “wrapper” does not add much (or any) functionality on its own, it is “thin”.

The default behaviors of arrays in C++ are driven by the fact that arrays are a reference type — the array variable is not a named storage location for the entire contents of the array, but rather it contains just enough information to locate the array in memory, and must be combined with an index to provide access to (part of) the logically enclosed data. This means that arrays seem to behave differently than most other data types in C++. Primitive, structured, and object types all have similar behavioral defaults. For example, they default to pass-by-value semantics with respect to function calls, while arrays default to pass-by-reference semantics.

If we wrap an array in a struct or class however — even a thin wrapper — we change the default behavior. Just by wrapping the array, we change it from being a reference type to either a structured type or object type. This means we get the following changes in default behavior (among others):

  • pass-by-value semantics with respect to function calls (VS the raw array pass-by-reference).
  • ability to perform (bitwise) assignment by default (VS the raw array being non-assignable).

In this assignment, you will experiment with object wrappers for arrays, with varying levels of added functionality.