Hi,
hope somebody has an idea how to get this done:
I have a console app with some "menu" which I want to handle in a "nice" way. My idea is to store a array of "pointers-to-members" for every menu level. After that I thought I can evaluate the user entries with a standard method that only cares about the menu level and automatically calls the selected method's.
Layout is at follows:
----------------------------------------------------------------
header file:
typedef struct somestruct {
int (*pointer-to-method)(void);
}
clase someclass {
public:
int display_menu_0();
int display_menu_1()
somestruct menulevel_0[4];
somestruct menulevel_1[2];
...
private:
int level_0_cmd_0 ( void );
int level_0_cmd_1 ( void );
int level_0_cmd_2 ( void );
int level_0_cmd_4 ( void );
int level_1_cmd_0 ( void );
int level_2_cmd_1 ( void );
}
cpp file:
// constructor
someclass::someclass {
...
// set first element to point to first method to call
// for this menulevel, something like this
someclass::menulevel_0[0] = someclass::level_0_cmd_0;
// and so on for the other ones
...
}
main file:
// calling in main should be something like this
temp = display_menu_0; // to get user input what to do
// now call the appropiate method using the array
menulevel_0[temp]();
-------------------------------------------------------
As some of you might already get, it didn't work that way. I tried various combinations of how to define/typedef the things.
Most problems seemed to be caused by the fact that the "somestruct" structure has some namespacing trouble about where the pointer actually points to. And the fact that an array of "pointers-to-methods" seems to be "not easy" to create. I can't get it to work.
Maybe some of you as an idea on this, or another suggestion on how to implement an multi level menu with as less code repetition as possible.
Thanks a lot for your time reading this.
Tobias Alte

Tricky pointer-to-member problem ( at least for me )
Roda Chan
Hi Tobias,
You really have quite a few misunderstandings about pointer to members, including basic syntax. I would encourage you to read up on them. Of course I would have to suggest the 4th Edition of the C++ Primer written by Stan Lippman (who is on my team), Josee Lajoie and Barbara Moo. Section 18.3 describes pointer to class members.
Anyway, the example below compiles and exexutes and is as close I could come to what I think you are trying to achieve.
Ronald Laeremans
Visual C++ team
#include <Windows.h>
class someclass;
typedef int (someclass::* p_someclass_member) ();
class someclass
{
public:
someclass();
int display_menu_0() {return 0;};
int display_menu_1() {return 0;};
p_someclass_member menulevel_0[4];
p_someclass_member menulevel_1[2];
private:
int level_0_cmd_0 ( ) {MessageBox(0, L"level_0_cmd_0", L"", 0); return 0;};
int level_0_cmd_1 ( ) {MessageBox(0, L"level_0_cmd_1", L"", 0); return 0;};
int level_0_cmd_2 ( ) {return 0;};
int level_0_cmd_4 ( ) {return 0;};
int level_1_cmd_0 ( ) {return 0;};
int level_2_cmd_1 ( ) {return 0;};
};
//cpp file:
// constructor
someclass::someclass()
{
// set first element to point to first method to call
// for this menulevel, something like this
someclass::menulevel_0[0] = &someclass::level_0_cmd_0;
someclass::menulevel_0[1] = &someclass::level_0_cmd_1;
// and so on for the other ones
}
//main file:
int main()
{
someclass sc;
// calling in main should be something like this
int temp = sc.display_menu_0(); // to get user input what to do
// now call the appropiate method using the array
((&sc)->*(sc.menulevel_0[0]))();
((&sc)->*(sc.menulevel_0[1]))();
}