title: Creating clone of an object not working with virtual base class
body: <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class Something
  {
    public:
    int j;
    Something():j(20) {cout&lt;&lt;"Something initialized. j="&lt;&lt;j&lt;&lt;endl;}
  };
  
  class Base
  {
    private:
      Base(const Base&amp;) {}
    public:
      Base() {}
      virtual Base *clone() { return new Base(*this); }
      virtual void ID() { cout&lt;&lt;"BASE"&lt;&lt;endl; }
  };
  
  class Derived : public Base
  {
    private:
      int id;
      Something *s;
      Derived(const Derived&amp;) {}
    public:
      Derived():id(10) {cout&lt;&lt;"Called constructor and allocated id"&lt;&lt;endl;s=new Something();}
      ~Derived() {delete s;}
      virtual Base *clone() { return new Derived(*this); }
      virtual void ID() { cout&lt;&lt;"DERIVED id="&lt;&lt;id&lt;&lt;endl; }
      void assignID(int i) {id=i;}
  };
  
  
  int main()
  {
          Base* b=new Derived();
          b-&gt;ID();
          Base* c=b-&gt;clone();
          c-&gt;ID();
  }//main
  </code></pre>
  
  <p>On running:</p>
  
  <pre><code>Called constructor and allocated id
  Something initialized. j=20
  DERIVED id=10
  DERIVED id=0
  </code></pre>
  
  <p>My question is related to <a href="https://stackoverflow.com/questions/3829220/is-there-no-way-to-upcast-into-an-abstract-class-and-not-modify-it-each-time-a-cl">this</a>, <a href="https://stackoverflow.com/questions/3063534/can-i-pass-a-pointer-to-a-superclass-but-create-a-copy-of-the-child">this</a> and <a href="https://stackoverflow.com/questions/3136646/how-can-a-derived-c-class-clone-itself-via-a-base-pointer">this</a> post.</p>
  
  <p>In the first link, Space_C0wb0y says </p>
  
  <blockquote>
    <p>"Since the clone-method is a method of
    the actual class of the object, it can
    also create a deep-copy. It can access
    all members of the class it belongs
    to, so no problems there."</p>
  </blockquote>
  
  <p>I don't understand how a deep copy can happen. In the program above, not even a shallow copy is happening. <strong>I need it to work even if the Base class is an abstract class</strong>. How can I do a deep copy here? Help please?</p>