I built this class for simple text based menu, for using with other classes.
#ifndef MENU_H
#define MENU_H
#include <string>
using std::string;
#include <vector>
using std::vector;
#include "misc.h"
template <class MyClass>
struct Option
{
string labbel;
void (MyClass::*function)(void);
};
template <class MyClass>
class Menu
{
private:
string title;
string exit_labbel;
vector< Option<MyClass> > options;
public:
// Constructor
Menu(const string& title, const string& exit_labbel);
// Destructor
~Menu(void){};
// Adicionar Opcao
void AddOption(const string& labbel, void (MyClass::*funcao)(void));
// Ligar o menu
void Start() const;
};
template <class MyClass>
Menu<MyClass>::Menu(const std::string &title, const std::string &exit_labbel):
title(title),
exit_labbel(exit_labbel)
{
}
template <class MyClass>
void
Menu<MyClass>::AddOption(const std::string &labbel, void (MyClass::*function)(void))
{
Option<MyClass> option;
option.labbel = labbel;
option.function = function;
options.push_back(option);
}
template <class MyClass>
void
Menu<MyClass>::Start() const
{
cout << endl << '[' << title << ']' << endl;
unsigned numoption = 1;
for(unsigned i = 0; i != options.size(); ++i)
{
cout << " * " << numopcao << " - " << options[i].labbel << endl;
numoption++;
}
cout << " * 0 - " << exit_labbel << endl;
std::cin >> input;
if( input == 0 )
return;
else
{
options[input - 1].function;
}
Start();
}
#endif
This thing compiles, and functions are working but when I choose some option, it just calls Start again without calling the option I choose.
Can someone help me :P ?