Archived

This topic is now archived and is closed to further replies.

Generalized Command Interpreter

This topic is 5965 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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*

Share this post


Link to post
Share on other sites
does that mean i pass a pointer to the class or to the current object using a this pointer?

WeP
http://www.planetzztpp.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Do you just need an array of things that can act as functions of any type? That''s fairly easy. Make a class with a pure virtual method, you''ll probably want to use call it something like exectute(), or maybe overload operator(). Then make subclasses, one for each function. Then put pointers to them in your array.
  
class Executable
{
public: virtual void Execute() = 0;
};

class StatusPrinter : public Executable
{
Data* d;
public: void Execute(){d->PrintStatus();}
};


that''s pretty much the main reason to use inheritance, see how easy that was?

If you want to implement parameters it shouldn''t be that hard, just put them the Execute function. The derived classes can decide whether to use them or not. Also the derived objects can have more members if they need them. Also I think there is a way of calling member functions but I think it only works for the constant ones. You''d use mem_funct, never used it myself but it ought to be in your standard library book (everyone needs one).

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites