title: Is it possible to change a C++ object's class after instantiation?
body: <p>I have a bunch of classes which all inherit the same attributes from a common base class. The base class implements some virtual functions that work in general cases, whilst each subclass re-implements those virtual functions for a variety of special cases. </p>
  
  <p>Here's the situation: I want the special-ness of these sub-classed objects to be expendable. Essentially, I would like to implement an <code>expend()</code> function which causes an object to lose its sub-class identity and revert to being a base-class instance with the general-case behaviours implemented in the base class.</p>
  
  <p>I should note that the derived classes don't introduce any additional variables, so both the base and derived classes should be the same size in memory.</p>
  
  <p>I'm open to destroying the old object and creating a new one, as long as I can create the new object at the same memory address, so existing pointers aren't broken.</p>
  
  <p>The following attempt doesn't work, and produces some seemingly unexpected behaviour. What am I missing here?</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class Base {
  public:
      virtual void whoami() { 
          std::cout &lt;&lt; "I am Base\n"; 
      }
  };
  
  class Derived : public Base {
  public:
      void whoami() {
          std::cout &lt;&lt; "I am Derived\n";
      }
  };
  
  Base* object;
  
  int main() {
      object = new Derived; //assign a new Derived class instance
      object-&gt;whoami(); //this prints "I am Derived"
  
      Base baseObject;
      *object = baseObject; //reassign existing object to a different type
      object-&gt;whoami(); //but it *STILL* prints "I am Derived" (!)
  
      return 0;
  }
  </code></pre>