title: C++: How do I pass a container of derived classes to a function expecting a container of their base classes?
body: <p>HI! Anyone know how I can make the line "<code>chug(derlist);</code>" in the code below work?  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;list&gt;
  using namespace std;
  
  class Base
  {
  public:
      virtual void chug() { cout &lt;&lt; "Base chug\n"; }
  };
  
  class Derived : public Base
  {
  public:
      virtual void chug() { cout &lt;&lt; "Derived chug\n"; }
      void foo() { cout &lt;&lt; "Derived foo\n"; }
  };
  
  void chug(list&lt;Base*&gt;&amp; alist)
  {
      for (list&lt;Base*&gt;::iterator i = alist.begin(), z = alist.end(); i != z; ++i)
          (*i)-&gt;chug();
  }
  
  int main() 
  {
      list&lt;Base*&gt; baselist;
      list&lt;Derived*&gt; derlist;
  
      baselist.push_back(new Base);
      baselist.push_back(new Base);
      derlist.push_back(new Derived);
      derlist.push_back(new Derived);
  
      chug(baselist);
      // chug(derlist);  // How do I make this work?
  
      return 0;
  }
  </code></pre>
  
  <p>The reason I need this is basically, I have a container of very complex objects, which I need to pass to certain functions that only care about one or two virtual functions in those complex objects.</p>
  
  <p>I know the short answer is "you can't," I'm really looking for any tricks/idioms that people use to get around this problem.</p>
  
  <p>Thanks in advance. </p>