title: How to initialize a generic base class in c++
body: <p>I am trying to rewrite some old code to make it more Object oriented. previously I have a <strong>union</strong> of <em>struct</em> called, say <code>Event</code></p>
  <pre><code>union Event {
    AEvent aEvent;
    BEvent bEvent;
    CEvent cEvent;
  }
  </code></pre>
  <p>and this <code>Event</code> is later used in a struct</p>
  <pre><code>struct EventImpl {
    ... //some other members 
    Event event();
  }
  </code></pre>
  <p>now I am trying to use the powerful <code>shared_ptr</code>;
  I created a new base class called <code>BaseEvent</code></p>
  <pre><code>class BaseEvent{
      size_t m_TypeHash; 
      public: 
          using Ptr = std::shared_ptr&lt;BaseEvent&gt;;
          virtual ~EventDesc() = default; 
          
          template &lt;class T&gt;
          // cast to different events
          static std::shared_ptr&lt;T&gt; Cast(const Ptr&amp; src) {
              return src-&gt;m_TypeHash == typeid(T).hash_code() 
                  ? std::static_pointer_cast&lt;T&gt;(src)
                  : nullptr;
          }
      protected: 
          BaseEvent(size_t typeHash) : m_TypeHash(typeHash){}
  };
  </code></pre>
  <p>From here, <code>AEvent</code>, <code>BEvent</code> and <code>CEvent</code> can just extend <code>BaseEvent</code>, which makes the code cleaner.</p>
  <p>But the question arises, what do I with the struct <code>EventImpl</code> that contains <code>Event</code>?? Is there a way to initialize a generic   <code>BaseEvent</code>?</p>
  <pre><code>struct EventImpl {
    ... //some other members 
    BaseEvent&lt;T&gt; event; //??
    T event; // ??
    std::shared_ptr&lt;BaseEvent&gt; event; // ???
  }
  
  </code></pre>
  <p><strong>edit</strong>:
  the name <code>EventImpl</code> is a little misleading, as Dmitry pointed out, <code>EventWrapper</code> deems to be more appropriate. :)</p>