Dealing with circular inclusion in a parent/child class relationship

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?

-------------Problems Reply------------

You'll have to use forward declaration:

class Child; //Forward declaration
class Parent
vector<Child*> m_children;

class Parent; //Forward declaration
class Child
Parent* m_parent;

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.

Category:c# Views:1 Time:2010-11-15

Related post

  • Parent Child Record Relationship in SQL? 2010-02-09

    I need to figure out the best way, in SQL, to query on a Parent / Child relationship. Some of the parent fields will be the data for the child. Here is an example: ID Field1 Field2 ParentId -------------------------------------------- 1 stuff moreStu

  • Parent child class relationship design pattern 2010-04-13

    I have a class which has a list of child items. Is there a design pattern I can copy that I can apply to these classes so that I can access the parent instance from the child, and it enforces rules such as not being able to add the child to multiple

  • parent/child project relationship in Mercurial 2011-04-01

    I'm new to mercurial (and SCM in general) and I'm stuck at handling a parent child relationship between my two projects. One project is the parent project and is a subset of the other project (or rather, the other one is a superset of the first). I w

  • Climbing a Parent/Child Database Relationship in Postgres 2009-02-04

    We have the following example table (actually taken from another example here on stackoverflow...) CREATE TABLE example ( id integer primary key, name char(200), parentid integer, value integer); And given a specific child we want to get the top Pare

  • iphone parent-child objects relationship 2011-05-26

    I have a "NewsViewController" which has a property named "postViewController" of type PostViewController. From inside the PostViewController I would like to call a method of the NewsViewController class. What is the easiest way to do this ? After goo

  • Best practice for parent/child-viewmodel-relationships in MVVM using Onyx? 2009-06-02

    Hy guys! I am currently working on a little WPF project using MVVM via the Onyx framework. My currentview architecture is like this: <DockPanel> <Menu DockPanel.Dock="Top" Background="#cecece"> <!-- Menu --> </Menu> <Grid

  • How to hold Parent-Child C# relationship with Entity Framework 2014-09-23

    not sure if anyone can help me in this complex situation, hoping for good. I am wondering if inheritance is the best way, maybe something else. I am clueless... How should I set the relationship between the classes? In the Database: I have a Table ca

  • PHP object parent/child recursion 2011-01-07

    I've got a parent-child OO relationship. Parent obejcts has many child objects and every child object knows about it's parent by reference. The parent can be a child too (basically its a tree). When i do a var_dump() on the root object it says ["pare

  • Reporting Services Aggregate not working for parent-child relationships 2009-05-22

    I have a parent-child attribute relationship in an Analysis services dimension, say [Dim].[Child] is a child of [Dim].[Parent] and I have a query, that returns the set UNION( [Dim].[Parent].Members * [Dim].[Child].Members, [Dim].[Parent].[ALL] * [Dim

  • Looping through an array and inserting into a table with parent child relationship 2010-06-23

    I have an array which looks like Array ( [438044691:maincategoryone] => Array ( [0] => Array ( [id] => 438044692 [name] => main category one item one [category] => Array ( [2] => Array ( [id] => 4380444456 [name] => main categ

  • Nhibernate 3.0 and Fluent 1.2 - Parent-child relationship. Session.Query does not fire a join query 2011-09-19

    I have 3 classes (names modified and simplified with get; set; etc.) - a parent-child composition relationship and an association public class ParentClass { public IList<ChildClass> Children {get;set;} } public class ChildClass { public ParentC

  • Reporting Services: Business object data source with parent-child-grandchild 2012-02-13

    How do I create a report with a parent-child-grandchild relationship using POCOs / custom business objects? public class Invoice { public List<Account> Accounts { get; set; } } public class Account { public List<LineItem> LineItems { get;

  • Many-to-many relationship on parent/child form is limiting data entry 2012-03-20

    I have an Access 2003 front-end that used to use an Access back-end without problem. I recently switched the Access backend to instead use SQL Server 2005, but I haven't been able to figure out this last issue yet - any help would be appreciated. I h

  • One-to-many, parent-child tables circular relationshp in MySQL 2011-05-16

    I'm facing this problem: I have a parent table, and a child table, one parent can have multiple children, standard story. These are the constraints: each parent must have at least one child, each parent must have one favourite child, each parent can

  • How to deal with NHibernate parent\child relationship with childs in ISet and generated IDs? 2010-02-18

    Having entered the world of NHibernate less than one year ago, I'm still developing my "personal" best practice and architectural solutions in this area... now I'm facing a pretty simple issue on which I'd like to have opinions or suggestions by some

  • Problem with parent/child relation with hibernate 2010-07-15

    I am facing a problem with parent child type relations. Hibernate docs say to add a "many-to-one" relation in child class to get value of foreign key from parent. But to make this relation work I have to add Invoice property in child class that intro

  • Best practice for Parent / Child UI design in ASP.Net? 2009-03-24

    Coming from a desktop client background, with no real data-driven web design experience, I am studying ASP.NET UI design to determine the best UI pattern for Parent/Children data. I always tend to experiment with Parent/Child presentation when learni

  • MySQL parent -> child query 2009-06-03

    I'm working with mySQL, and I'm in a situation where I need to select data from one table that matches an ID at any level in parent -> child data hierarchy in the other table. Further more, I would like to resolve this with a well written SQL quer

  • Domain Driven Design - Parent child relation pattern - Specification pattern 2010-01-28

    I was wondering which of the following is considered to be a best practice when dealing with parent child relationships. 1) The following example seems to be a common practice, but when creating an instance of a child, it will be in an invalid state

Copyright (C), All Rights Reserved.

processed in 0.160 (s). 11 q(s)