Assume I've made a class, say
Parent, that has a composition relation with
Child. The parent class holds a list of children.
I want all children to hold a reference to the parent, so every child holds a
This will cause circular inclusion. I refer to
Child in parent.h and I refer to
Parent in child.h. Therefore
Parent will need to include
Child, which needs to include
What's the best way to work around this?
You'll have to use forward declaration:
class Child; //Forward declaration
class Parent; //Forward declaration
Since only a pointer of
Parent is stored inside the
Child class there is no need to do a
#include "parent.h" in the
child.h file. Use the forward declaration of
class Parent; in
child.h instead of inclding
parent.h in there. In the source file of child i.e.
child.cpp you can do
#include "parent.h" to use the