Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>#include &lt;functional&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;map&gt; class api { // maps containing the different function pointers typedef void(*voidfuncptr)(); typedef int(*stringcrintptr)(std::string, const int&amp;); std::map&lt;std::string, voidfuncptr&gt; voida; std::map&lt;std::string, stringcrintptr&gt; stringcrint; public: // api temp class // given an api and a name, it converts to a function pointer // depending on parameters used class apitemp { const std::string n; const api* p; public: apitemp(const std::string&amp; name, const api* parent) : n(name), p(parent) {} operator voidfuncptr() { return p-&gt;voida.find(n)-&gt;second; } operator stringcrintptr() { return p-&gt;stringcrint.find(n)-&gt;second; } }; // insertion of new functions into appropriate maps void insert(const std::string&amp; name, voidfuncptr ptr) { voida[name]=ptr; } void insert(const std::string&amp; name, stringcrintptr ptr) { stringcrint[name]=ptr; } // operator[] for the name gets halfway to the right function apitemp operator[](std::string n) const { return apitemp(n, this); } }; </code></pre> <p>Usage: </p> <pre><code>api myMap; int hello_world(std::string name, const int &amp; number ) { name += "!"; std::cout &lt;&lt; "Hello, " &lt;&lt; name &lt;&lt; std::endl; return number; } int main() { myMap.insert("my_method_hello", &amp;hello_world ); int a = myMap["my_method_hello"]("Tim", 25); } </code></pre> <p><a href="http://ideone.com/SXAPu" rel="nofollow">http://ideone.com/SXAPu</a> Not very pretty. Better advice is to not do anything even remotely like whawtever it is you're trying to do.</p> <p>Note that this requires all functions with the same parameters to return the same type.</p>
 

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