title: Best practice for nested polymorphism in C++?
body: <p>I have a slightly complex model which I'm currently refactoring in an attempt to make the code cleaner and easier to maintain. The model contains a main bit of functionality and then different models. Each model again has two classes, one for storage (data) and the second one for model execution (helper). The helper classes are instantiated millions of times throughout the main loop but depend on the data class.</p>
  
  <p>Right now I have approached the code like this:</p>
  
  <pre><code>main() {
  Base* dataptr = chooseInstance(int x);
  Base&amp; data = *dataptr;
  // do stuff with data
  };
  
  Base* chooseInstance(x) { /* returns instance of A:Base or B:Base */ };
  </code></pre>
  
  <p>Subsequently I have a helper class specific to A or B, which needs to be called repeatedly throughout the program. Currently I am handling this so:</p>
  
  <pre><code>...
  HelperBase* helper = data.getHelper(); // returns custom *HelperA or *HelperB class
  helper-&gt;doStuff();
  ...
  </code></pre>
  
  <p>This approach works well enough, but it doesn't feel very elegant. Is this something that would better be handled otherwise with templates or such?</p>
  
  <p>I realise that this question is a little vague as it's more a matter of style than a particular right/wrong issue - apologies for that.</p>