title: Specify the right object and implementation of a virtual function in a macro
body: <p>I have an abstract class DNXServo and classes XL320 and AX12A which inherit the abstract one and are concrete. Each object of the concrete classes I have in my main corresponds to a serial port on an Arduino Mega and I have 6 servos with unique IDs hooked on each serial port. For the sake of clarity in my code I have the ID of each servo associated with a macro, such as:</p>
  
  <pre><code>    // SERIAL 3- hips - AX12A object
      #define HIP_LEFT_FRONT          11
      #define HIP_LEFT_MIDDLE         12
      #define HIP_LEFT_BACK           13
      #define HIP_RIGHT_FRONT         14
      #define HIP_RIGHT_MIDDLE        15
      #define HIP_RIGHT_BACK          16
  
      // SERIAL 2 - knees - XL320 object
      #define KNEE_LEFT_FRONT         17
      #define KNEE_LEFT_MIDDLE        18
      #define KNEE_LEFT_BACK          19
      #define KNEE_RIGHT_FRONT        20
      #define KNEE_RIGHT_MIDDLE       21
      #define KNEE_RIGHT_BACK         22       
  etc.
  </code></pre>
  
  <p>Currently my main looks like this: </p>
  
  <pre><code>void setup(){
  // Call constructors
      XL320 arms(Serial1, 112500);
      XL320 knees(Serial2, 112500);
      AX12A hips(Serial3, 112500); 
  // Write to Servo
      int pos=SetGoalPosition(KNEE_LEFT_FRONT, 250);
  }
  
  
  //return pointer to the object associated with the proper Serial
  DNXServo* lookUpServo(int ID){
      if (ID&gt;=11 &amp;&amp; ID&lt;=16) return &amp;hips;
      else if (ID&gt;=17 &amp;&amp; ID&lt;=22)  return &amp;knees;
      else return &amp;arms;
  }
  
  int SetGoalPosition(int ID, int position){
          DNXServo * servo_ptr = lookUpServo(ID);
          return servo_ptr-&gt;SetGoalPosition(ID,position);
  }
  </code></pre>
  
  <p>SetGoalPosition is actually a virtual member function in DNXServo class and has different concrete implementations in XL320 and AX12A. The case is the same with many other member functions.The problem with my current implementation is that I have to define an additional function in the main for each member function.</p>
  
  <p>Can I avoid that using macros? Ideally, I would like to write sth like</p>
  
  <pre><code>SERVO(KNEE_LEFT_FRONT).SetGoalPosition(250);
  </code></pre>
  
  <p>where SetGoalPosition is the name of any member function and automatically call lookUpServo and then call the right implementation of the virtual function.</p>
  
  <p><strong>EDIT</strong>: <code>int XL320::SetGoalPosition(int ID, int position)</code> is the actual definition of the function. Note that it takes two arguments. Therefore I would like <code>SERVO(KNEE_LEFT_FRONT).SetGoalPosition(250);</code> to be expanded to sth like <code>hips.SetGoalPosition(KNEE_LEFT_FRONT, 250);</code> . As suggested in comments, reference can work as well, but then I would need to always type the ID twice <code>SERVO(KNEE_LEFT_FRONT).SetGoalPosition(KNEE_LEFT_FRONT, 250);</code></p>