title: Using native c++ wrappers for ncurses, How can I create a menu/submenu system?
body: <p>I spent the last year learning C++ (using C++ Primer as the recommended material from <a href="https://stackoverflow.com/a/388282/1440199">https://stackoverflow.com/a/388282/1440199</a>). I wanted to learn to use ncurses to make my first flying-solo program. Even though NCurses is C-based, it is more or less not well known that <a href="https://github.com/mirror/ncurses/tree/master/c%2B%2B" rel="nofollow noreferrer">native ncurses C++ wrappers exist</a>. However, the documentation for them are almost nonexistent; there's a demo.cc file Ive been using as a basis, and a <a href="https://reversed.top/2016-04-24/ncurses-for-cpp/" rel="nofollow noreferrer">few places around the web</a>. Anyways, Ive made it my final exam, so to speak, to understand and use these wrappers.</p>
  
  <p>The problem I am encountering is of the "best programming practices" type: Because of the lack of documented examples, I'm unsure of the best way to create a submenu from the demo's <a href="https://github.com/mirror/ncurses/blob/master/c%2B%2B/demo.cc#L432" rel="nofollow noreferrer">initial menu system</a>. </p>
  
  <p>The best idea I can come up with is to use the <a href="https://github.com/mirror/ncurses/blob/master/c%2B%2B/demo.cc#L449" rel="nofollow noreferrer">example way</a> to <a href="https://github.com/mirror/ncurses/blob/master/c%2B%2B/cursesm.h#L592" rel="nofollow noreferrer">attach ncurses++'s NCursesUserItem to a menu item</a>. Would the <a href="http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/menus.html#MENUUSERPTR" rel="nofollow noreferrer">User Pointer</a> be a pointer to the previous menu class? In NCurses, you can post and unpost menus, so I am also assuming I want to 'unpost' the previous menu when the new one 'posts', thats why I wanted a pointer to the previous menu upon showing the new one.</p>
  
  <p>The typesafe example way to attach userdata(for set_item_userptr) to a menu item:</p>
  
  <pre><code>template&lt;class T&gt; class SubMenu : public NCursesUserItem&lt;T&gt;
  {
    public:
      SubMenu (const char* p_name,
               const T* p_UserData)
              : NCursesUserItem&lt;T&gt;(p_name, static_cast&lt;const char*&gt;(0), p_UserData){}
  
      virtual ~SubMenu() {}
  
      bool action() {
          // I believe I want to unpost the previous menu here
          //  before posting the new submenu?
          NCursesUserItem&lt;T&gt;::UserData()-&gt;prev_menu()-&gt;unpost();
          //This doesn't work, but its kind of what I want to do.
          NCursesUserItem&lt;T&gt;::UserData()-&gt;post();
          return FALSE;
      }
  };
  </code></pre>
  
  <p>The Main menu object. I created the Submenu object here and passed 'this' (the main menu pointer) as an argument to it.</p>
  
  <pre><code>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&lt;Choice0Menu&gt; ("Choice 0", u);
          I[1] = new PadAction("Choice 1");
          I[2] = new QuitItem();
          I[3] = new NCursesMenuItem();
  
          InitMenu(I, TRUE, TRUE);
      }
  };
  </code></pre>
  
  <p>Choice0Menu class, which should contain a back button for returning to the Main Menu (I[2])</p>
  
  <pre><code>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;
      }
  };
  </code></pre>
  
  <p>I just have a lot of doubts about this way of doing it, for example, maybe I dont have to pass the entire 'this' pointer to the main menu when creating the submenu, just the post and unpost members. And MainMenu and Choice0Menu classes are very similar in function, so maybe even make it into one template class? As Choice0Menu takes a MainMenu as an argument, maybe it should be T* to take any menu type..</p>
  
  <p>My method doesn't seem the 'clean' way to program the problem.</p>
  
  <p>Thanks for the help. I have been studying this problem for almost 2 weeks now...</p>