title: Avoiding many typecasting
body: <p>I'm creating a UI system for my OpenGL application and I have this design...</p>
  
  <p>The UI manager:</p>
  
  <pre><code>class UIManager {
  public:
    UIElement *add(UIElement *element);
  }
  </code></pre>
  
  <p>Example element:</p>
  
  <pre><code>class Button : public UIElement {
  public:
    Button(UIElement *parent, const string &amp;caption, const Point &amp;position);
  }
  </code></pre>
  
  <p>Now everytime I want to instantiate a button and keep the pointer to it, I'll have to write this:</p>
  
  <pre><code>// somewhere in the header file
  UIManager *manager;
  Button *button1, *button2;
  
  // in cpp
  button1 = static_cast&lt;Button *&gt;(manager-&gt;add(new Button(0, "Enter", Point(10, 10))));
  button2 = static_cast&lt;Button *&gt;(manager-&gt;add(new Button(0, "Exit", Point(10, 50))));
  </code></pre>
  
  <p>which looks a bit lengthy to me, especially when I have a dozen of elements.</p>
  
  <p>Is this a bad design? Is there another way to get over this?</p>