I was coding this up in C# and the quickest solution to come to mind used the "as" or "is" keywords. I began wondering how I could implement it neatly in C++(without RTTI)... or even in C# without the aforementioned keywords.
Here is the problem (simplified):
There is a class Command
which contains a stream of so called "tokens".
class Command
{
public List<Token> Toks {get; set;}
//...
}
A token may (currently) be a "keyword" or a "game object".
class Token
{
//nothing in here :(
}
class KWToken : Token
{
public List<string> Synonyms {get; set;}
}
class GOToken : Token
{
}
Later, I'll want to loop through a Command
object's list of Token
s and do stuff based on the kind of Token
stored.
The thorn in this scenario is the fact that KWToken
objects contain a List
of associated strings that I require.
Of course, as said before, the simple solution in C# would use the "is" or "as" keyword.
I thought of a few not-so-sexy ways to do it.
Anyone have pretty ideas?
EDIT
(Printing example removed as it seemed to be misleading)
The list of synonyms in the KWToken object will be accessed in many places, through the associated Command object. Not just once for printing them as I might have implied with my example.