title: Resolving circular reference variable dependency in C++
body: <p>Suppose i have following code for making a data flow architecture with C++</p>
  
  <pre><code>class DataflowClass {
      public:
          DataflowClass(int &amp; _a, int &amp; b) : a(_a), b(b){}
  
          inline void calculate(){
              c = a + b;
              d = a - 2*b;
          }
  
          inline int &amp; getC() { return c; }
          inline int &amp; getD() { return d; }
  
      private:
          int c = 1;
          int d = 2;
          int &amp; a;
          int &amp; b;
  };
  
  void TestReferenceVariableDataflow(int count){
  
      int a;
      int b;
  
      DataflowClass c1(a, b);
      DataflowClass c2(c1.getC(), c1.getD());
      DataflowClass c3(c2.getC(), c2.getD());
  
      for(int i=0;i&lt;count;i++){
          a = c3.getC();
          b = c3.getD();
          c1.calculate();
          c2.calculate();
          c3.calculate();
      }
  }
  </code></pre>
  
  <p>The DataflowClass is a main building for dataflow blocks which have two inputs and two outputs. The constructor of DataflowClass accepts two reference variables as inputs and has two getter functions which return the reference to its two output members,
  As can be seen, in TestReferenceVariableDataflow function i constructed 3 instances of DataflowClass, the c1 instance constructed with a reference to two local variables defined in TestReferenceVariableDataflow function, the c2 has been constructed with two references comming from c1.getC() and c2.getC() and vice versa for c3.</p>
  
  <p>in each iteration, the calculate method of 3 classes will be called defined order, which result in dataflow computation within the 3 classes.</p>
  
  <p>The real problem is circular dependency, as you can see, the c1 constructed with reference of two variables defined in TestReferenceVariableDataflow function and at the end of each iteration, i have to copy the values from c3.getC() and c3.getD() into those variables.</p>
  
  <p>as the DataflowClass accepts reference variables in its constructor, i can't connect the output variables of c3 directly to c1, because the c3 instance isn't available at the construction time of c1 and so i have to define two extra variable to copy the values in each iteration from the last to the first.</p>
  
  <p>as you know, the classes with reference members should initialize the reference at the construction time and this issue makes that circular dependency problem.</p>
  
  <p>Is there any solution to this circular dependency problem? </p>
  
  <p><strong>Edit</strong>
  There are some simple reasons behind this design,</p>
  
  <p>1- first of all, this is design only uses C++ generic syntax and not any other sort of libraries or even std::*, just because of performance, think there are tens of these blocks, and all connected to each other serially, 
  another reason to avoid using libraries, is this code is part of real time software and its behavior is much more predictable than libraries</p>
  
  <p>2- The instances of DataflowClass will never destroy and we can't have dangling pointers</p>
  
  <p>3- In time critical and realtime softwares, dynamic memory management are somehow prohibited, because of unpredictability and this design truly could be used with all static memory allocation</p>
  
  <p>4- Use of pointers are not considered as good solution, because with the pointers static compile time assertion can't be achieved, the compile time wiring prevents unconnected input nets
  Also the pointers may get null or something like that</p>
  
  <p>5- this design doesn't need change signals for each variables, because all calculate methods will be called serially, and also maybe called automatically with a parent class</p>
  
  <p>6- The inputs of blocks may come from different blocks (than their previous blocks), the provided example is just a simple case which the blocks connected to each other uniformly</p>
  
  <p>7- Simple this design would be a C++ implementation of simulink blocks, the output of blocks are connected to the input of other blocks and they will calculate their outputs with increment of time</p>