0

I've read in this question that

If you really have a model where A contains B and B contains A then these classes appear to be incapable of living without each other. In which case perhaps you only really have one class and not two.

What would the case be if A may contain B, but B should always have a reference to A (B cannot exist without A)?

To illustrate, heres a Cube class that may contain a object of Collider class (IE: other cubes with colliders should collide with it).

class Cube
{
public:
    Cube();
    ~Cube();

    Collider collider; //Optional Collider
}

class Collider
{
public:
    Collider(Cube & parentReference) : parent(parentReference) {}

private:
    Cube & parent;
}

In a case such as this, where Cube and Collider should really be two seperate classes, how does one avoid circular dependencies while still be able to store a reference to Cube in Collider?

Edit: This question has since been answered. However, is anyone kind enough to let me know why this question was downvoted, such that I would change how I ask in the future? Duplicate? Not enough info? Too much of a beginner question? Do let me know :)

Community
  • 1
  • 1
eclmist
  • 161
  • 1
  • 14
  • In your `class Cube` I would either use a `Collider *collider` and check it for null or use `boost::optional optionalCollider`. – SAlex Sep 27 '16 at 10:19
  • Either way, wouldn't both collider and cube class need to include the headers of each other? Wouldn't that still result in circular dependencies? – eclmist Sep 27 '16 at 10:22
  • Yes, but as styko wrote in an answer below, you can add a forward declaration to fix the issue of going circular. – SAlex Sep 27 '16 at 10:24
  • The two answers posted after your comment clarified it for me, thanks – eclmist Sep 27 '16 at 10:26
  • Just wondering: can only cubes collide? The other elements in your "system" dont need a "collider"? – GhostCat Sep 27 '16 at 13:16

2 Answers2

3
class Collider;

class Cube
{
public:
    Collider* collider; //Optional Collider
}

class Collider
{
public:
    Collider(Cube & parentReference) : parent(parentReference) {}

private:
    Cube & parent;
}

Declare the existance of Collider and define it later. C(++) allows pointers to incomplete types. Here's more info about forward declaration.

Community
  • 1
  • 1
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
-1

You can do forward declaration. See wikipedia and this question.

For the code for your example, see the answer by @Ivan Rubinson.

Community
  • 1
  • 1
styko
  • 641
  • 3
  • 14
  • 1
    Why the downvote? Shall I just have commented Ivan Rubinson answer to add the name "forward declaration"? – styko Sep 27 '16 at 10:38
  • I'm extremely confused and frustrated with the downvotings as of late as well. People should at least let the posters know why. Thanks for helping too! – eclmist Sep 27 '16 at 10:42
  • I guess it's because your answer is short and lacks substance? SO community can be harsh at times. – Ivan Rubinson Sep 27 '16 at 11:09