title: Share data between classes without dynamic memory allocation
body: <p>I am working on a C++ project which has been created by C developers years ago.</p>
  <p>First of all you have to know some restrictions I have in this project:</p>
  <ul>
  <li>no dynamic memory allocation is allowed</li>
  <li>all objects are generated in a global &quot;builder&quot; file. In this file the objects are also connected by references when calling the constructors.</li>
  </ul>
  <p>I have some issues sharing data between objects because of the architecture. Let me give you an example:</p>
  <p>A.h</p>
  <pre><code>class A
  {
    private:
      dataStructA data;
  }
  </code></pre>
  <p>B.h</p>
  <pre><code>class B
  {
    private:
      dataStructB data;
  }
  </code></pre>
  <p>C.h</p>
  <pre><code>class C
  {
    private:
      dataStructC data;
  }
  </code></pre>
  <p>D.h</p>
  <pre><code>class D
  {
    private:
      dataStructD data;
  }
  </code></pre>
  <p>The objects are all responsible for some data structures which need to be accessed by the other objects. Therefore I have to connect them in the previously mentioned builder file like:</p>
  <p>builder.cpp</p>
  <pre><code>A(&amp;B, &amp;C);
  B(&amp;D);
  C(&amp;A, &amp;D); //and so on...
  </code></pre>
  <p>This is really ugly so I want to ask you how I can do it better.</p>
  <p>My first approach was to specify one of the classes as my main class which holds references to all other classes:</p>
  <p>A.h</p>
  <pre><code>class A
  {
    private:
      dataStructA data;
      B&amp; b;
      C&amp; c;
      D&amp; d;
  }
  </code></pre>
  <p>The subclasses then must also know the main class (A in this example) to access the other data structures. Sadly doing this without dynamic memory allocation results in circular dependencies and lot of problems. So I stopped it.</p>
  <p>How would you solve this problem?</p>
  <p>Extra questions: How would you solve it WITH dynamic memory allocation? Maybe I get a good arguement to refactor it in the future.</p>
  <p>Thanks and greetings</p>