title: Why must unused virtual functions be defined?
body: <p>I find it quite odd that unused virtual functions must still be defined unlike unused ordinary functions. I understand somewhat about the implicit <em>vtables</em> and <em>vpointers</em> which are created when a class object is created - this somewhat answers the question (that the function must be defined so that the pointers to the virtual function can be defined) but this pushes my query back further still. </p>
  
  <p>Why would a <em>vtable</em> entry need to be created for a function if there's absolutely no chance that virtual function will be called at all?</p>
  
  <pre><code>class A{
      virtual bool test() const;
  };
  
  int main(){
      A a; //error: undefined reference to 'vtable for A'
  }
  </code></pre>
  
  <p>Even though I declared <code>A::test()</code> it was never used in the program but it still throws up an error. Can the compiler not run through the program and realise <code>test()</code> was never called - and thus not require a <em>vtable</em> entry for it? Or is that an unreasonable thing to expect of the compiler?</p>