Pointers

12.5 - Object relationship using pointer


One reason to use raw pointers is to define relationship between object. In the example the Person object has a Person* member called father. This defines a relationship a Person object with another Person object.

Task

  • Print out the name of george's father.
    • Use george.father->name to access the name.
#include <iostream> struct Person { std::string name; Person* father; }; int main() { Person jonathan{"Jonathan", nullptr}; Person george{"George", &jonathan}; Person joseph{"Joseph", &george}; }