- An ordinary variable refers to the location of an object. A
pointer variable refers to the address of the location of an object.
- Pointers generally take far less memory than do class objects.
- If you want to define a pointer variable,
then instantiate the following pattern:
data type *pointer name
- If you want to allocate a chunk of memory at run time,
and you want to assign the address of that chunk of memory
to a pointer variable,
then instantiate the following pattern:
pointer name = new data type;
- If you want to access a member variable in a class object,
and you have a pointer to that class object,
then instantiate one of the following patterns:
(*pointer name).member variable name // OK
pointer name -> member variable name // Better
- If you want to write a value into the memory reserved for
a member variable in a class object,
and you have a pointer to that class object,
then instantiate one of the following
patterns:
// OK:
(*pointer name).member variable name = expression
// Better:
pointer name -> member variable name = expression