Skip to main content
GameDev.net gamedev.net
🔒 Locked

Generalized Command Interpreter

Started by wep Aug 10, 2001 at 8:50 AM 5 replies 650+ views
Original Post
wep
wep
hi, suppose i have a program (called zzt++) written in C++ which contains a number of classes, many of which contain a command loop: a loop reading characters from the keyboard and calling functions accordingly using a switch(c){ case: ... } statement. i am trying to eliminate the use of a command loop in every class by creating a single class which does this work for all other classes requiring a command loop. i do this by first filling an array with pointers to functions and then filling another array with the corresponding user actions required to call the functions. the generalized command interpreter then reads the keyboard, matches the user''s key presses and calls the appropriate function. sounds cool, doesn''t it? and it works, except for these two things: 1. i can only pass pointers to static member functions in C++. static memeber functions can only use static data members. 2. it is not possible to pass parameters in this way. who knows another (better) way of doing this? any input will be appreciated. WeP http://www.planetzztpp.com
WePhttp://www.planetzztpp.com
ironfroggy
ironfroggy
This is one of the greatest limitations of C++ that I think could have been done alot better. Only way around it I know is to create a CLASS_METHOD() function with parameters CLASS* and then the normal params of CLASS::METHOD() and just have CLASS_METHOD() call the correct method from the CLASS*
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
wep
wep
does that mean i pass a pointer to the class or to the current object using a this pointer?

WeP
http://www.planetzztpp.com
WePhttp://www.planetzztpp.com
wep
wep
anonymous poster,

great idea. thanks for the input...

wep

WeP
http://www.planetzztpp.com
WePhttp://www.planetzztpp.com
GayleSaver
GayleSaver
Yep, that''s the OO way of doing it, or you could go a step further and have a hash table to map the commands and functions.

Anyway, you could just use plain old function pointers and procedures if you don''t want to make it too complicated.

Or you could do something more advanced...
VK
wep
wep
well,

there are intricacies here...

i would like a class (BM) to first set up two arrays: one with pointers to functions and one with possible key presses.

at some point class BM passes control to our new Generalized Command Interpreter class (GCI). when a user presses a key, GCI knows which function to call. when any other class does this (with differently filled arrays ofcourse) GCI would still know what to do. the problem is parameters...because pointers to functions are passed once at the beginning, parameters cannot be passed while GCI is in its command loop.

food for thought...i''m still thinking about the inheritance thing....



WeP
http://www.planetzztpp.com
WePhttp://www.planetzztpp.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.