- If you must be prepared to store a large number of class objects in the
worst case, and the expected number of class objects is much smaller, and
you want to conserve memory, then define an array of pointers to class
objects, instead of an array of class objects.
- If you want to create a one-dimensional array of class-object pointers,
then instantiate the following pattern:
class name *array name [maximum number of objects];
- If you want to allocate a chunk of memory at run time,
and you want to refer to the address of that chunk of memory
in a pointer array,
then use the
new
operator and instantiate the
following pattern:
array name[index] = new class name;
- If you want to write a value into the memory reserved for a member variable in a class object,
and you have an array that contains a pointer to that class object,
then instantiate one of the following patterns:
// Ok:
(*array name[index]).member variable name = expression;
// Better:
array name[index] -> member variable name = expression;
- If you want to access a member variable in a class object,
and you have an array that contains a pointer to that class object,
then instantiate one of the following patterns:
(*array name[index]).member variable name // OK
array name[index] -> member variable name // Better