title: Communication between objects in C++
body: <p>I have two classes A and B which are defines as follows:</p>
  
  <pre><code>class A
  {
      public:
      void *connector;
  };
  
  class B
  {
      public:
      void *connector1;
      void *connector2;
  };
  </code></pre>
  
  <p>To start, let us assume that I create three objects C1, C2 and C3 based on these classes,</p>
  
  <pre><code>A C1;
  B C2;
  A C3;
  </code></pre>
  
  <p>And I use the following code to connect them</p>
  
  <pre><code>C1.connector = &amp;C2;
  C2.connector1 = &amp;C1;
  C2.connector2 = &amp;C3;
  C3.connector = &amp;C2;
  </code></pre>
  
  <p>So at the moment I have this:  C1 &lt;-> C2 &lt;-> C3 (first example). </p>
  
  <p>Important: The reason I am using void pointers inside all the classes is because I cannot predict right from the start how the objects will connect. For example,the answer to this question should be valid if I create a fourth object and make these connections (C1 and C2 the same as before):</p>
  
  <pre><code>B C3;    
  A C4;
  
  C1.connector = &amp;C2;
  C2.connector1 = &amp;C1;
  C2.connector2 = &amp;C3;
  C3.connector1 = &amp;C2;
  C3.connector2 = &amp;C4;
  C4.connector = &amp;C3;
  </code></pre>
  
  <p>Which corresponds to this C1 &lt;-> C2 &lt;-> C3 &lt;-> C4 (second example).</p>
  
  <p>I tried and searched everywhere for a simple solution to implement communication between these objects. There are even several questions with the same name but I could not find a similarity between what was asked and respective answers to the problem I am trying to solve. Maybe because I am not approaching this correctly right from the start. Anyway, by communication I mean a simple way that C2 can call a method in C1 and vice versa. </p>
  
  <p>1) I tried to do it with templates but could not figure out how to do it. All classes would be templates and that seamed really contrived and complicated (hard to read, maintain and implement). </p>
  
  <p>2) I tried to use casting of pointers but since I don't know right form the start to which class they connect I could not do it.</p>
  
  <p>Important: Although I cannot warranty the connections I can live very well with the fact that, for example, an object of class B knows exactly the name of the method it will execute in the object connected to its connectors. </p>
  
  <p>To make it clear, let us assume that we introduce a method</p>
  
  <pre><code>int get_value(); 
  </code></pre>
  
  <p>in both class A and class B (but the implementation is different in each class). So, in the second example above, C2 may call it on C1 and C3 may call this method on C2. But note that C2 and C3 are objects of the same class.</p>
  
  <p>Ok, now that I explained my problem, I would like to ask for a simple solution for the communication in this problem. For example, a working piece of code that implements the communication between C2 and C1 but also between C3 and C2 (see example 2 above). </p>
  
  <p>To be specific I think an example in which object C2/C3 can call the get_value method in C1/C2, such that C2/C3 can store the value in a private/public variable would be perfect.</p>
  
  <p>I realize that the way I am trying to implement this may not be the best and answers that solve the problem on a different approach are also welcome. But if possible please show me some code and a little explanation because my main target and expertise is not the programming skills.</p>
  
  <p>Thank you very much in advance.</p>