我去年学习C ++(使用C ++ Primer作为https://stackoverflow.com/a/388282/1440199的推荐材料)。我想学习使用ncurses制作我的第一个飞行独奏程序。即使NCurses是基于C的,native ncurses C++ wrappers exist还是多少不是众所周知的。但是,关于它们的文档几乎不存在。我已经使用过一个demo.cc文件作为基础,还有一个few places around the web。无论如何,可以说,我已经完成了我的期末考试,以了解和使用这些包装器。
我遇到的问题属于“最佳编程实践”类型:由于缺少记录的示例,我不确定从演示的initial menu system创建子菜单的最佳方法。
我能想到的最好的主意是使用example way至attach ncurses++'s NCursesUserItem to a menu item。 User Pointer是否会指向先前的菜单类?在NCurses中,您可以发布和取消发布菜单,所以我还假设我想在新菜单“发布”时“取消发布”上一个菜单,这就是为什么我希望在显示新菜单时指向上一菜单。 >
将用户数据(用于set_item_userptr)附加到菜单项的类型安全示例方法:
template<class T> class SubMenu : public NCursesUserItem<T>
{
public:
SubMenu (const char* p_name,
const T* p_UserData)
: NCursesUserItem<T>(p_name, static_cast<const char*>(0), p_UserData){}
virtual ~SubMenu() {}
bool action() {
// I believe I want to unpost the previous menu here
// before posting the new submenu?
NCursesUserItem<T>::UserData()->prev_menu()->unpost();
//This doesn't work, but its kind of what I want to do.
NCursesUserItem<T>::UserData()->post();
return FALSE;
}
};
主菜单对象。我在这里创建了子菜单对象,并将“ this”(主菜单指针)作为参数传递给了它。
class MainMenu : public NCursesMenu
{
private:
NCursesMenuItem** I;
Choice0Menu* u;
#define n_items 3
public:
MainMenu ()
: NCursesMenu (n_items+2, 25, (lines()-5)/2, (cols()-23)/2), I(0), u(0)
{
u = new Choice0Menu(this);
I = new NCursesMenuItem*[1+n_items];
I[0] = new SubMenu<Choice0Menu> ("Choice 0", u);
I[1] = new PadAction("Choice 1");
I[2] = new QuitItem();
I[3] = new NCursesMenuItem();
InitMenu(I, TRUE, TRUE);
}
};
Choice0Menu类,其中应包含一个用于返回主菜单(I [2])的后退按钮
class Choice0Menu : public NCursesMenu
{
private:
NCursesMenuItem** I;
MainMenu* w;
#define n_items 3
public:
Choice0Menu (MainMenu* prev)
: NCursesMenu (n_items+2, 25, (lines()-5)/2, (cols()-23)/2), I(0), w(prev)
{
I = new NCursesMenuItem*[1+n_items];
I[0] = new ScanAction("Submenu Choice 0");
I[1] = new PadAction("Submenu Choice 1");
I[2] = new MainMenuAction("Go Back");
I[3] = new NCursesMenuItem();
InitMenu(I, TRUE, TRUE);
// returns the pointer to the prev object
MainMenu* prev_menu() const{
return w;
}
};
我只是对这样做的方式有很多疑问,例如,也许我在创建子菜单时不必将整个“ this”指针传递给主菜单,而只需传递成员和未成员即可。而且MainMenu和Choice0Menu类在功能上非常相似,因此也许甚至可以将其合并为一个模板类?由于Choice0Menu将MainMenu作为参数,所以对于任何菜单类型,应该为T *。
我的方法似乎不是编程问题的“干净”方法。
感谢您的帮助。我已经研究这个问题近两个星期了……