title: Selecting which derived class to use at compile time
body: <p>I think this is a very simple question, but I don't know enough C++ to make it sound simple.</p>
  
  <p>I have code that defines an abstract base class <code>ContinuousDistribution</code>, and derived classes such as <code>Cauchy</code> or <code>Normal</code>.<br>
  My goal is to have a default definition of a variable <code>A</code>, but allow the user to change that definition and, in particular, change the type of the variable. 
  (I don't mean run-time re-definition, it's compile-time re-definition)</p>
  
  <p>So in the default file I would have</p>
  
  <p><strong>default.cpp</strong>  </p>
  
  <pre><code>... 
  Normal A(0., 20.);  // Normal is the default
  ....
  x = A.logpdf();
  </code></pre>
  
  <p>which compiles and runs assuming the default <code>Normal</code> distribution.</p>
  
  <p>Then I want the user to create a "configuration" file in which the definition of <code>A</code> can be changed. In another file, <em>which will be compiled together with</em> <code>default.cpp</code>, we could have</p>
  
  <p><strong>user1.cpp</strong></p>
  
  <pre><code>... 
  Normal A(0., 10.);  // change the arguments of Normal
  ....
  call some functions defined in default.cpp, which use methods of A
  ....
  </code></pre>
  
  <p>or another one with </p>
  
  <p><strong>user2.cpp</strong></p>
  
  <pre><code>... 
  Cauchy A(0., 10.);  // change the type of A
  ....
  call some functions defined in default.cpp, which use methods of A
  ....
  </code></pre>
  
  <p>To solve this I tried using <code>extern Normal A</code> in <code>default.cpp</code> but that doesn't allow the variable to be redefined as <code>Cauchy</code>. 
  I also tried with <code>extern ContinuousDistribution A</code> in <code>default.cpp</code>, which also doesn't work.
  If the definition of <code>A</code> is removed from <code>default.cpp</code> then I cannot compile with <code>error: ‘A’ was not declared in this scope</code>, because I use <code>A.logpdf()</code>.</p>
  
  <p>How can I solve this?</p>
  
  <p>As suggested in a comment, I also tried to use a pointer like
  <code>Normal *A=new Normal(0,20);</code> in <code>default.cpp</code> and reassign it with <code>A = new Cauchy(0,10);</code> in <code>user2.cpp</code>, but then <code>user2.cpp</code> doesn't compile because <code>A was not declared in this scope</code>.</p>