title: Design pattern for class compatibility
body: <p>I would like to know which would be the <strong>most efficient</strong> (regarding speed) way to solve the following problem:</p>
  
  <p>Let's say we have a class <code>AFoo</code>, having a simple header file as:</p>
  
  <p>AFoo.h</p>
  
  <pre><code>class AFoo {
  public:
      double x;
      //Additional methods/variables belonging only to AFoo
  };
  </code></pre>
  
  <p>Now, I receive a set of classes that <strong>I do not define</strong> (i.e. some other stubborn programmer is creating those classes), which all "share" the <code>x</code> variable. We could imagine that one member of such set is the class <code>BFoo</code>, which looks like:</p>
  
  <p>BFoo.h</p>
  
  <pre><code>class BFoo {
  public:
      double x;
      //Additional methods/variables belonging only to BFoo
  };
  </code></pre>
  
  <p>Now, I have a function somewhere which accepts some pointer to an <code>AFoo</code></p>
  
  <pre><code>void doSomething(AFoo *pnt){
      //does operations with the x member only.
  }
  </code></pre>
  
  <p>I would really like that my <code>doSomething</code> function accepts also a pointer to a <code>BFoo</code> or any class of the set I was given. The abstraction of <code>x</code> into a new class <code>Foo</code> is no solution because the stubborn programmer refuses to inherit from it. I thought of using template functions, but the inconvenient is that I would have to recompile every time the stubborn programmer creates a new class.</p>
  
  <p>My first try was to define an interface which behaves the following way:</p>
  
  <pre><code>class AlternativeFoo {
  public:
      virtual double &amp; getX() = 0;
  };
  </code></pre>
  
  <p>so I can define another class like</p>
  
  <pre><code>class AlternativeAFoo: public AlternativeFoo{
  public:
      double &amp; getX(){ return mPoint.x; };
  private:
      APoint mPoint;
  };
  </code></pre>
  
  <p>Now I can change my <code>doSomething</code> method to accept a pointer to an <code>AlternativeFoo</code> variable, which works fine. However, this solution is noticeably slower than template classes. Is there a design pattern that I am missing which does not compromise performance? Thank you very much!</p>
  
  <p>EDIT:
  As a concrete example of the problem, imagine I code a library which I want to be compatible with several definitions of the same abstract idea, for example, a 2D point which has coordinates x and y. This definitions can come from APIs in video game design software or whatever, and they all share the fact that they have <code>x</code> and <code>y</code> coordinates as member variables of the same type. How can I make a piece of code compatible with all of them?</p>