title: Use instance of abstract class in cross-platform code
body: <p>Consider the situation, when you create cross-platform multi-thread application.</p>
  
  <p>So, when you do that, you probably would divide the platform functionality between some classes. For example, you need simple console to output some text in your application. The possible way you do this is declare some abstract class <code>Console</code> and than, when you need it on some platform, inherit from it and use derived classes instances.</p>
  
  <p>Everything is OK till you find out, that the <code>Console</code> instance is needed somewhere(For example as class member). You know, that if you try to declare a member variable of class <code>Console</code> in some other class, you would get an error, because the instances of abstract classes are not allowed. But you still do need that class, as you consider that on compilation the derived class will be implemented and it will be correct, but you don't know the name of that class, or there may be different names depending on platform.</p>
  
  <p>Here is the complicated example.</p>
  
  <p>When you develop cross-platform multi-thread application, you need something like <code>Syncer</code>(Synchronizer). The analog of it is the mutex object on Windows API. </p>
  
  <p>You know that depending on platform that class would be different. But you do know that there are some methods that any platform variant will has. For example these methods are <code>lock</code> and <code>unlock</code>. As you don't know how will these methods be done, you declare them pure virtual as next:</p>
  
  <pre><code>class Syncer
  {
  public:
      virtual void lock()=0;
      virtual void unlock()=0;
  }
  </code></pre>
  
  <p>And now, developing some class, you realize, that you need not one, but a lot of this <code>Syncer</code>-s. But you can't do that, as the class is abstract.</p>
  
  <p>The possible solutions I've found are:</p>
  
  <p>1) Use templates. For example:</p>
  
  <pre><code>template&lt;class syncer&gt;
  class SyncUser
  {
  public:
      syncer syncInstance;
      SyncUser()
      {
          syncInstance().lock;
  }
  </code></pre>
  
  <p>But the problem is that the class user may paste instead of <code>syncer</code> parameter the any other class.(Would he will or not, it doesn't matter. He can do this). So that solution is not as good as the general solution is expected. </p>
  
  <p>2) Use macros. If using this way, the contents of <code>Syncer.h</code> would be like next:</p>
  
  <pre><code>class Syncer
  {
  public:
      virtual void lock()=0;
      virtual void unlock()=0;
  }
  #ifdef PLATFORM1
  #include "platform1_sync_implement.h"
  #else
  #include "platform2_sync_implement.h"
  #endif
  </code></pre>
  
  <p>when the <code>"platform1_sync_implement.h"</code> file has the following:</p>
  
  <pre><code>#include "Syncer.h"
  
  class SyncPlatform1 : public Syncer
  {
      //Correct implementation
  }
  
  #define Syncer SyncPlatform1
  </code></pre>
  
  <p>Now, anyone included <code>Syncer.h</code> would have the the implemented version.
  But this way is also not good, as requires the list of platforms, where the <code>Sync</code> abstraction will be implemented. That is not so good for big projects, as you just need to declare that class and care about that other programmers could use it.</p>
  
  <p><strong>So the question is: is there a better way to use the 'instance' of abstract class, knowing that its child class would be implemented somewhere.</strong></p>
  
  <p>P.S.: I know that that is not the best name for such question.
  P.P.S.: Sorry for bad English.</p>