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.
I define a parent and child type class:
class token{...};
class word: public token{...};
Class token shares a variable with word; word further refines token. Neither class is virtual.
I have a helper function of the following signature, with representative creation code indicated:
token
maker()
{
...
case ...: // (1)
return token(...);
case ...: // (2)
return word(...);
...
},
the idea being obviously that it creates the appropriate type, and returns it as a token.
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
token t = maker(); // I want to be able to use the methods of word, if
// maker actually made a word (true polymorphic behavior)
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.