previous | start | next

## Common Uses for Pointers (Sharing)

- Another common use of pointers is sharing.
- Rather than duplicating objects, we can use pointers to share the object.
- Pointers are useful to model a "n:1" relationship, in which a number of different variables share the same object.
- Example: Many departments may have a receptionist and a secretary. In some departments, this it the same person.

  >     class Department
  >     {
  >      . . .
  >     private:
  >        string name;
  >        Employee* receptionist;
  >        Employee* secretary;
  >     };

- Sharing is particularly important when changes to the object need to be observed by all users of the object.

  >     Employee* tina = new Employee("Tester, Tina", 50000);
  >     Department qc("Quality Control");
  >     qc.set_receptionist(tina);
  >     qc.set_secretary(tina);
  >     tina->set_salary(55000);

- Without using pointers, changing Tina's salary would not update the information in the receptionist or secretary attribute.

previous | start | next