title: Avoiding dynamic_cast in implementation of virtual functions in derived class
body: <p>Here is some sample code explaining what I am trying to achieve.</p>
  
  <p>Basically, I have an algorithm that depends on some basic operations available in a class. I have defined those operations in a pure abstract base class. I want to apply that algorithm to a variety of objects that provide those operations by deriving classes for the specific objects.</p>
  
  <p>However, the different derived objects are incompatible with one another as far those operations are concerned. My question is whether I can avoid using RTTI to ensure that for example, bool derived2::identical(const base* other2), asserts(or other exit mechanism) where other2 is not of type derived2.</p>
  
  <p>One alternative would be to template the function algorithm on the specific derived object, but that would mean that it's implementation would have to live in a header file which I don't want to do since 1) Changing the algorithm code for test purposes can cause recompilation of large portions of the code 2) The algorithm's implementation would be exposed in the header instead of living nicely in a source file hidden from the end-user.</p>
  
  <h2>Header file</h2>
  
  <pre><code>#include &lt;list&gt;
  
  class base
  {
  public:
   virtual float difference(const base*) const = 0;
   virtual bool identical(const base*) const = 0; 
  };
  
  
  class derived1 : public base
  {
   public:
   float difference(const base* other1) const
   {
    // other1 has to be of type derived1
              if(typeid(other1) == typeid(this))
              {
                      // process ...
              }
              else
              {
                      assert(0);
              }
    return 1;
   }
  
   bool identical(const base* other1) const
   {
    // other1 has to be of type derived1
              if(typeid(other1) == typeid(this))
              {
                      // compare...
              }
              else
              {
                      assert(0);
              }
    return true;
   }
  };
  
  
  class derived2 : public base
  {
   public:
          float difference(const base* other2) const
          { 
               // process ...
    // other2 has to be of type derived2
              return 2;
          }
  
   bool identical(const base* other2) const
          {
                  // do comparison
    // derived1 and derived2 cannot be compared
                  return true;
          }
  };
  
  // Declaration
  int algorithm(std::list&lt;base*&gt;&amp; members);
  </code></pre>
  
  <h2>Implementation of algorithm Source file</h2>
  
  <pre><code>#include "header_file_containing_base"
  int algorithm(std::list&lt;base*&gt;&amp; members)
  {
   // This function only relies on the interface defined in base
   // process members;
  
   return 1;
  }
  </code></pre>
  
  <h2>Main program</h2>
  
  <pre><code>int main()
  {
    // Create lists of derived1 and derived2
    // Run algorithm on these lists
  }
  </code></pre>