Note that there are some explanatory texts on larger screens.

plurals
  1. POOn writing win32 api wrapper with C++, how to pass this pointer to static function
    text
    copied!<p>I want to convert function object to function.</p> <p>I wrote this code, but it doesn't work.</p> <pre><code>#include &lt;iostream&gt; typedef int (*int_to_int)(int); struct adder { int n_; adder (int n) : n_(n) {} int operator() (int x) { return x + n_; } operator int_to_int () { return this-&gt;*&amp;adder::operator(); } }; int main(void) { adder add_two(2); int_to_int add_fn = add_two; std::cout &lt;&lt; add_two(3) &lt;&lt; std::endl; // expect 5 std::cout &lt;&lt; add_fn(3) &lt;&lt; std::endl; // expect 5 add_fn = adder(5); std::cout &lt;&lt; add_fn(3) &lt;&lt; std::endl; // expect 8 return 0; } </code></pre> <p>and I got message from g++, says <code>invalid use of non-static member function</code>.</p> <p>How do I get pointer to member function of instance?</p> <p><strong>Edit</strong>:My original problem is about Win32 API.</p> <p>I'm forced to write windows program with old Win32 API at school. but I don't want to write horrible switch statement like some example codes on text. Then, I decided to write wrapper in C++.</p> <p>I want to write the window class like ... </p> <pre><code>class Window { public: LRESULT update (HWND, UINT, WPARAM, LPARAM); void run(); // below methods are called by update() virtual void onclick(int, int); virtual void ondraw(); // ... and more methods }; </code></pre> <p>and I'm willing to write my application class with deriving this class and overloading some methods.</p> <p>Finally, my actual probrem is how to do this in C++.</p> <pre><code> // on initializing my window object, // I must register window class with callback which is // not a C++ function object. // I try to explain what I want to do with psudocode mixing C++ and javascript. WNDCLASS wndclass; Window *self = this; wndclass.lpfnWndProc = function () { return self.update.apply(self, argunemts); }; </code></pre> <p>In other word, I have to make closure into function pointer. I don't know how to do this, but I can't believe this can't do in C++.</p> <p><strong>Edit</strong>: The original title of this question was <em>How to get pointer-to-member-function of instance</em>. But this title and my question didn't tell my actual problem enough. Sorry to early answerers, and litb, thank you for suggestion and very useful answer!</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