title: Preserving polymorphism in class hierarchies (C++)
body: <p>Something like this must have been asked on SO before, but I failed to track down an answer directly addressing the below. It is a very basic question, but, away from my books, I'm blanking.</p>
  
  <p>I define a parent and child type class:</p>
  
  <pre><code>class token{...};  
  
  class word: public token{...};  
  </code></pre>
  
  <p>Class token shares a variable with word; word further refines token. Neither class is virtual.  </p>
  
  <p>I have a helper function of the following signature, with representative creation code indicated:</p>
  
  <pre><code>token  
  maker()  
  {  
  ...  
  case ...: // (1)
    return token(...);
  case ...: // (2)
    return word(...);
  ...
  },  
  </code></pre>
  
  <p>the idea being obviously that it creates the appropriate type, and returns it as a token. </p>
  
  <p>I mean to use the returned object by statically assigning it to a token type, with dynamic resolution to the true type (word or token) when appropriate, as in</p>
  
  <pre><code>token t = maker(); // I want to be able to use the methods of word, if 
                     // maker actually made a word (true polymorphic behavior)
  </code></pre>
  
  <p>I realize that this should be by pointer or reference to enable polymorphism, and that in the current design maker() slices word types to token types; but am blanking on how to do adjust the above best to achieve my goal. Yes, I am embarassed. </p>