5

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 Parent pointer.

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 Parent.

What's the best way to work around this?

Pieter
  • 31,619
  • 76
  • 167
  • 242
  • Why do the children need parent pointers? This is probably a design flaw. – John Dibling Nov 15 '10 at 12:46
  • The children will also be used outside the `Parent` class. The code that uses the children doesn't know at first which parent they're assigned to. – Pieter Nov 15 '10 at 13:01
  • Not having seen the code, I'd say there's a good chance John is right and this is a design flaw. – T.E.D. Nov 15 '10 at 13:05
  • @Pieter: I realize that you've accepted an answer and probably moved on with your life, but you should really reconsider your design. If `Parent`s need to know about `Child`s and `Child`s need to know about `Parent`s, then something is not right, and it's going to come back to haunt you later when its 10 times harder to fix than it is now. – John Dibling Nov 15 '10 at 15:32
  • @John: My question relates to a project that I'm working on for my C++ class. Unfortunately, I am not allowed to discuss the design or code of my program with anyone other than the professor and her assistants, so I can only ask generalized abstract questions. I'd like to investigate the alleged design flaw, but I cannot disclose any details about my code so I'm afraid that will be impossible. – Pieter Nov 15 '10 at 16:16
  • By the way, I've seen children keep a parent pointer before. Many classes in the Qt framework have a constructor that lets you supply a parent pointer. Example: http://doc.trolltech.com/4.7/qgraphicsscene.html – Pieter Nov 15 '10 at 16:21
  • @Pieter: I understand your constraints. Your professor is not doing you any favors by teaching you to program with very a poor design. This is a great example of why in the past 10 years I've never been able to hire a programmer straight from college that I didn't have to extensively retrain. I'm not upset with you, keep in mind. I'm upset with your professor. – John Dibling Nov 15 '10 at 17:40

2 Answers2

8

You'll have to use forward declaration:

//parent.h
class Child; //Forward declaration
class Parent
{
    vector<Child*> m_children;
};

//child.h
class Parent; //Forward declaration
class Child
{
    Parent* m_parent;
};
Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114
  • I would not recommend the `vector` a `vector` would mean that the `Parent` owns the `Child`ren and would make memory management much easier. Of course there's still the issue of getting the copy constructor and assignment operator right because the `Child::m_parent` should be changed. – Matthieu M. Nov 15 '10 at 12:50
  • The parent could own the children even if they're declared as `Child*`. You'll of course have to provide correct value semantics for the `Parent` class. – Andreas Brinck Nov 15 '10 at 12:58
3

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 Parent methods.

Naveen
  • 74,600
  • 47
  • 176
  • 233