<p>Say I have:</p>

<pre><code>void Render(void(*Call)())
{
    D3dDevice-&gt;BeginScene();
    Call();
    D3dDevice-&gt;EndScene();
    D3dDevice-&gt;Present(0,0,0,0);
}
</code></pre>

<p>This is fine as long as the function I want to use to render is a function or a <code>static</code> member function:</p>

<pre><code>Render(MainMenuRender);
Render(MainMenu::Render);
</code></pre>

<p>However, I really want to be able to use a class method as well since in most cases the rendering function will want to access member variables, and Id rather not make the class instance global, e.g.</p>

<pre><code>Render(MainMenu-&gt;Render);
</code></pre>

<p>However I really have no idea how to do this, and still allow functions and <code>static</code> member functions to be used.</p>

[{'answer_id': 60512, 'author': 'mweerden', 'author_id': 4285, 'author_profile': 'https://Stackoverflow.com/users/4285', 'pm_score': 2, 'selected': False, 'text': '<p>You can make a wrapper function <code>void Wrap(T *t)</code> that just calls <code>t-&gt;Call()</code> and have <code>Render</code> take such a function together with an object. That is:</p>\n\n<pre><code>void Wrap(T *t)\n{\n  t-&gt;Call();\n}\n\nvoid Render(void (*f)(T *), T *t)\n{\n  ...\n  f(t);\n  ...\n}\n</code></pre>\n'}, {'answer_id': 60513, 'author': 'David Joyner', 'author_id': 1146, 'author_profile': 'https://Stackoverflow.com/users/1146', 'pm_score': 5, 'selected': True, 'text': '<p>There are a lot of ways to skin this cat, including templates.  My favorite is <a href="http://www.boost.org/doc/libs/1_36_0/doc/html/function.html" rel="nofollow noreferrer">Boost.function</a> as I\'ve found it to be the most flexible in the long run.  Also read up on <a href="http://www.boost.org/doc/libs/1_36_0/libs/bind/bind.html" rel="nofollow noreferrer">Boost.bind</a> for binding to member functions as well as many other tricks.</p>\n\n<p>It would look like this:</p>\n\n<pre><code>#include &lt;boost/bind.hpp&gt;\n#include &lt;boost/function.hpp&gt;\n\nvoid Render(boost::function0&lt;void&gt; Call)\n{\n    // as before...\n}\n\nRender(boost::bind(&amp;MainMenu::Render, myMainMenuInstance));\n</code></pre>\n'}, {'answer_id': 60515, 'author': 'cjanssen', 'author_id': 2950, 'author_profile': 'https://Stackoverflow.com/users/2950', 'pm_score': 1, 'selected': False, 'text': '<p>I did so once by defining a global function "Call" which accepts a pointer to your intance as member</p>\n\n<pre><code>void CallRender(myclass *Instance)\n{\n  Instance-&gt;Render();\n}\n</code></pre>\n\n<p>So render becomes:</p>\n\n<pre><code>void Render(void (*Call)(myclass*), myclass* Instance)\n{\n  ...\n  Call(Instance);\n  ...\n}\n</code></pre>\n\n<p>And your call to render is:</p>\n\n<pre><code>Render(CallRender, &amp;MainMenu);\n</code></pre>\n\n<p>I know it\'s ugly, but worked for me (I was using pthreads)</p>\n'}, {'answer_id': 60527, 'author': 'Tom Martin', 'author_id': 5303, 'author_profile': 'https://Stackoverflow.com/users/5303', 'pm_score': 1, 'selected': False, 'text': '<p>You can\'t call a member function from a pointer unless you have a reference to the object as well.  For example:\n<pre><code>((object).*(ptrToMember))</pre></code></p>\n\n<p>So you won\'t be able to acheive this without changing the signature of your render method.  <a href="http://www.parashift.com/c++-faq-lite/pointers-to-members.html" rel="nofollow noreferrer">This</a> article explains why this is generally a bad idea.</p>\n\n<p>A better way might be to define a "Renderer" interface which your classes that have render methods can implement and have that be the parameter type of your main Render method.  You could then write a "StaticCaller" implementation to support the calling of your static methods by reference.</p>\n\n<p>eg (My C++ is really rusty, I haven\'t compiled this either).</p>\n\n<pre><code>\n\nvoid Render(IRenderer *Renderer)\n{\n    D3dDevice->BeginScene();\n    Renderer->Render();\n    D3dDevice->EndScene();\n    D3dDevice->Present(0,0,0,0);\n}\n\n// The "interface"\npublic class IRenderer \n{\npublic:\n    virtual void Render();\n};\n\npublic class StaticCaller: public IRenderer\n{\n    void (*Call)();\npublic:\n\n    StaticCaller((*Call)())\n    {\n        this->Call = Call;\n    }\n\n    void Render()\n    {\n        Call();\n    }\n};\n\n</code></pre>\n\n<p>All this is pretty boilerplate but it should make for more readability.</p>\n'}, {'answer_id': 60549, 'author': 'Marcin Gil', 'author_id': 5731, 'author_profile': 'https://Stackoverflow.com/users/5731', 'pm_score': 2, 'selected': False, 'text': '<p>What about what <a href="http://www.parashift.com/c++-faq-lite/pointers-to-members.html" rel="nofollow noreferrer">C++ FAQ: Pointers to members</a> says?</p>\n'}, {'answer_id': 354514, 'author': 'Nick Gebbie', 'author_id': 44761, 'author_profile': 'https://Stackoverflow.com/users/44761', 'pm_score': 0, 'selected': False, 'text': "<p>You can declare a function pointer to a member function of class T using:</p>\n\n<pre><code>typedef void (T::*FUNCTIONPOINTERTYPE)(args..)\nFUNCTIONPOINTERTYPE function;\n</code></pre>\n\n<p>And invoke it as:</p>\n\n<pre><code>T* t;\nFUNCTIONPOINTERTYPE function;\n(t-&gt;*function)(args..);\n</code></pre>\n\n<p>Extrapolating this into useful currying system with variable arguments, types, return values, etc, is monotonous and annoying. I've heard good things about the aforementioned boost library, so I'd recommend looking into that before doing anything drastic.</p>\n"}]
['https://Stackoverflow.com/questions/60507', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6266/']