previous | start | next

## Pointers and Memory Allocation

- Pointers are declared by placing an `*` after the typename.

  >     Time * deadline;

- Pointers hold the location of an object, not the actual object itself.
- To create a new object, you use the `new` command.

  >     deadline = new Time;

- You can also call the `new` command in conjunction with a constructor to initialize the object.

  >     Employee* boss = new Employee("Lin, Lisa", 68000);

- To access the object that a pointer points to, the object must be dereferenced:
  - Use `*` to access the actual object.
  - Use `->` to access methods of the object.

  >     raise_salary(*boss, 10);
  >     string name = boss->get_name();

- The dot operator has a higher precedence than the `*` operator:

  >     string name = *boss.get_name(); // Error
  >     string name = (*boss).get_name(); // OK

previous | start | next