Using objects and object functions through variables
Background of the program: the user is able to input a string of two words
only - a verb and a noun. I tokenize this string into a vector and compare
tokens[0] with a vector of allowed verbs, and tokens[1] with a vector of
allowed nouns.
Now, I'm trying to find a way to allow only certain verbs to be performed
on certain nouns. For example, writing "take book" will give a message (or
whatever) saying it's allowed, but writing "take door" would not be. I
have so far created a class Object with bool values for each possible verb
(eg. within the class Object, I can create an Object book for which m_take
= true, or false for an Object door).
However, I'm having trouble associating these objects with the user input.
For example, I would like to be able to do something like this:
1) User inputs "verb noun", which go into the tokens vector as tokens[0]
and tokens[1].
2) Program checks if the input contains acceptable words (individually).
3) Considering getstat() to be the function to retreive the bool value of
the possible action doable on an object, the program retrieves
tokens[1].getstat(tokens[0]) and, if true, executes tokens[0].tokens[1]()
(eg. book.take()). This way I could have only one if cycle in my main(),
which can be used by all legal verbs and nouns, without making an infinite
list of if, else if, etc, considering every single option manually.
Sorry if this is at all confusing. I know it is not possible to use a
variable as an object name, but I'm sure there's a better way to do this
than doing cycles within cycles of considering every single mix and match
of verb and noun. I'm experimenting with like 3 each at the moment, but
once I get this working I plan on expanding it and it would be a nightmare
to keep track of every change if I have to hard-code every possible verb
and noun multiple times within the source code. (Also, sorry for not
posting the whole source - it's a really long file just now!) Thanks for
any help/hint in the right direction!
No comments:
Post a Comment