Note that there are some explanatory texts on larger screens.

plurals
  1. POPass member function pointers by argument and store them
    primarykey
    data
    text
    <p>I'm trying to tackle OOP with a little project of mine. I'm trying to create a simple Menu class (terminal-based) to build, show and execute a menu. And this menu should execute functions from a second class Calendar which is the "main" program. </p> <p>Which interface I try to achieve is:</p> <pre><code>int main() { Menu menu; menu.add("Exit program", &amp;Calendar::exit); menu.show(); menu.execute(menu.EXIT); // or just 0 } </code></pre> <p>Which should look something like:</p> <blockquote> <p>CALENDAR MENU</p> <p>0 Exit program</p> </blockquote> <p>What I want to achieve is the add()-method should add a function-pointer to a map&lt;int, function-pointer&gt;. This is where my problem starts. Currently I have a hard-coded array with function-pointers in my execute()-method. Now I want to dynamically add a menu-item with the appropriate function.</p> <pre><code>class Menu { // declaration of function-pointer thingy typedef void (Calendar::*CalendarPtr)(); // will hold the string in the add()-method std::vector&lt;std::string&gt; options_strings; // will hold the integer associated to the appropriate function std::map&lt;int, CalendarPtr&gt; options_map; public: // possible options enum OPTIONS {EXIT}; // shows the menu on-screen void show(); // should add string to vector, function-pointer to map void add(std::string, CalendarPtr); // should execute appropriate function (based on option passed) void execute(OPTIONS option); }; // ... void Menu::add(std::string option, CalendarPtr) // what doesn't work obviously { // adding string to vector options_strings.push_back(option); // Just binding the latest entry of the string to the function-pointer int index = options_strings.size(); // TRYING to add function-pointer to map options_map.insert(std::pair&lt;int, CalendarPtr&gt;(index, CalendarPtr)); } Menu::execute(OPTIONS option) // enum declared as public in class { int option = EXIT; // or 0 CalendarPtr cptr[] = {&amp;Calendar::exit}; Calendar* cal; if (option &gt;= 0 &amp;&amp; option &lt; static_cast&lt;int&gt;(options_strings.size())) (cal-&gt;*cptr[option])(); } </code></pre> <p>The insert-line currently gives:</p> <blockquote> <p>../src/Menu.cpp:24: error: expected primary-expression before ';' token</p> </blockquote> <p>which means that my use of function-pointers isn't right, but what is the right way? How would I declare the wanted add()-method (so which parameters), how would I insert the function-pointers in the map and how would I call the desired item in the map with my execute()-method?</p> <p>If something is not clear, please say it and I'll try to explain it better/different :)</p> <p>Thanks in advance!</p> <p><strong>EDIT 1:</strong></p> <p>I changed</p> <pre><code>void Menu::add(std::string option, CalendarPtr) // ... options_map.insert(std::pair&lt;int, CalendarPtr&gt;(index, CalendarPtr)); </code></pre> <p>to</p> <pre><code>void Menu::add(std::string option, CalendarPtr cptr) // ... options_map.insert(std::pair&lt;int, CalendarPtr&gt;(index, cptr)); </code></pre> <p>But how do you "call" the function at the inserted (say index 0) position? options_map0; doesn't work...</p> <p><strong>EDIT 2:</strong></p> <p>It inserted the function-pointer with index 1 ;) Problem solved!</p> <p><strike>I get an error when I have this method:</p> <pre><code>void Menu::execute(OPTIONS option) { Calendar c; Calendar* calendar = &amp;c; CalendarPtr cptr = options_map.at(0); (*calendar.*cptr)(); } </code></pre> <blockquote> <p>terminate called after throwing an instance of 'std::out_of_range' what(): map::at</p> </blockquote> <p>So I conclude that the insert failed, why so?</strike></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload