title: How to implement serialization in C++
body: <p>Whenever I find myself needing to serialize objects in a C++ program, I fall back  to this kind of pattern:</p>
  
  <pre><code>class Serializable {
    public:
      static Serializable *deserialize(istream &amp;is) {
          int id;
          is &gt;&gt; id;
          switch(id) {
            case EXAMPLE_ID:
              return new ExampleClass(is);
            //...
          }
      }
  
      void serialize(ostream &amp;os) {
          os &lt;&lt; getClassID();
          serializeMe(os);
      }
  
    protected:
      int getClassID()=0;
      void serializeMe(ostream &amp;os)=0;
  };
  </code></pre>
  
  <p>The above works pretty well in practice. However, I've heard that this kind of switching over class IDs is evil and an antipattern; what's the standard, OO-way of handling serialization in C++?</p>