title: Is checking of object type really always sign of bad design?
body: <p>I have a source of some lines of text, each of which is a message, representing object of some type. I'm making a parser for these lines, which should take the text line as input and give the ready to use object as output. So I make the following hierarchy of classes:</p>
  
  <pre><code>class Message
  {
  public:
      virtual ~Message(){};
  };
  
  class ObjectTypeA : public Message
  {/*...*/};
  class ObjectTypeB : public Message
  {/*...*/};
  class ObjectTypeC : public Message
  {/*...*/};
  </code></pre>
  
  <p>and here's how it's used:</p>
  
  <pre><code>std::shared_ptr&lt;Message&gt; parseLine(std::string&amp; line);
  
  void doWork()
  {
      std::string line;
      while(getLine(line))
      {
          std::shared_ptr&lt;Message&gt; object=parseLine(line);
          if(dynamic_cast&lt;ObjectTypeA*&gt;(object.get()))
              doSomethingA(*static_cast&lt;ObjectTypeA*&gt;(object.get()));
          else if(dynamic_cast&lt;ObjectTypeB*&gt;(object.get()))
              doCompletelyUnrelatedProcessing(*static_cast&lt;ObjectTypeB*&gt;(object.get()));
          else if(dynamic_cast&lt;ObjectTypeC*&gt;(object.get()))
              doSomethingEvenMoreDifferent(*static_cast&lt;ObjectTypeC*&gt;(object.get()));
      }
  }
  </code></pre>
  
  <p>Here the parser would be a library function, and the objects don't know in advance how they will be processed. So, I can't put the processing code to a virtual function of <code>Message</code> implementations.</p>
  
  <p>But many of the answers in <a href="https://stackoverflow.com/questions/351845">this question</a> say that if one needs to check type of the object, it's a sign of bad design. But I can't seem to see what's bad here. Is there any better way to organize the solution?</p>