Hints and tips for a little menu class

Still on my quest of creating a nice little console framework for myself. Today I am starting some work to help improve my programs flow of control. I thought a nice little intro to this would be a "Menu" class, that creates and handles a menu in the console, where the user can select from a few options, and the menu will call the appropriate function(s).

Here's what I personally came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
#include <functional>

#include "D:\mega\mega.h"

using namespace mega;

class MenuItem
{
private:
    int key;
    std::string name;
    std::function<void(void)> handle;
public:
    MenuItem() {}
    MenuItem(const int& k, const std::string& n, const std::function<void(void)>& h): key(k), name(n), handle(h) {}
    void Call() { handle(); }
    const int& GetKey() { return key; }
    const std::string& GetName() { return name; }
};

class Menu
{
private:
    std::vector<MenuItem> menu_container;
public:
    Menu(std::vector<std::string> names, std::vector<std::function<void(void)>> handles)
    {
        if(names.size() != handles.size()) throw std::runtime_error("Menu: Constructor arguments not equal");
        for(size_t i = 0; i < names.size(); i++)
            menu_container.push_back(MenuItem(i+1,names[i],handles[i]));
    }
    void Show()
    {
       while(1)
       {
           for(size_t i = 0; i < menu_container.size(); i++)
                ConsolePrint(menu_container[i].GetKey(),".) ",menu_container[i].GetName(),"\n");
            ConsolePrint("\n0.) Return/Exit\n\n");
            int input = GetConsoleInput<int>("Option: ");
            if(input == 0) return;
            if(input > 0 && input <= menu_container.size()) // Still trying to think of a nice way to incorporate range finding in GetConsoleInput
                menu_container[input-1].Call();
            else ConsolePrint("\n\nError: Invalid option.");
            ConsolePrint("\n\n");
        }
    }
};

void PlayNow() { ConsolePrint("Playing a game now.."); }

int main()
{
    ConsolePrint("----- ANiceTitle - Version 0.0.1 -----","\n");
    ConsolePrint("------- Developed by megatron_ -------","\n\n");
    Menu menu1({"Play now"},{PlayNow});
    menu1.Show();
    return 0;
}



Would anybody like to share some improvement/suggestions?
Topic archived. No new replies allowed.