Please send questions to
st10@humboldt.edu .
WHEN you write a class that uses dynamic allocation
within its methods, you are EXPECTED to also include
the following three special kinds of methods:
* destructor
* called FOR YOU when your object goes outside of scope
* its responsibility is to FREE dynamically-allocated memory
(and sometimes other clean-up)
* a class has ONLY one destructor,
its name is ~class_name (and no arguments)
* copy constructor
* needed when C++ would make a copy of something --
say, for a pass-by-value parameter
* name is the class name,
BUT with one parameter: an instance of the class
passed by reference but passed as a const (so
it cannot be changed)
* overloaded assignment operator
* what does assignment mean for this particular class?
* write a method with special name operator =
with one parameter: a const pass-by-reference element of
that class
and (tradition) have it return a reference to an object
of that type (so chaining assigmment statements can work)
* consider string_list -- a list of strings implemented with the
help of a dynamically-allocated array as one of its data fields,
and it has the "big 3" above (a destructor, a copy constructor,
and an overloaded assignment operator)
* another C++ syntax note:
keyword static
// static: inside of a class definition, it means there
// is EXACTLY ONE COPY of that thing no matter
// HOW many instances of that class are created
* we see an example of this in class string_list, in
string_list.h -- by making the named constant
INITIAL_CAP static:
static const int INITIAL_CAP = 10;
...now there will be ONLY one instance of
this named constant, no matter how many (or even
IF any) instances of the object have been
created;
* it can be used for more than just named constants... 8-)
* (then started a code-reading exercise/example, reading through
and discussing the string_list class, starting with string_list.h)