Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update</strong>: You told us what you want. I found this question here on SO: <a href="https://stackoverflow.com/questions/117792/best-method-for-storing-this-pointer-for-use-in-wndproc">Best method for storing this pointer for use in WndProc</a> . I'm not a Windows Programmer, but the Adam Rosenfield guy seem to be right in using SetWindowLongPtr and GetWindowLongPtr. So, you use it like this:</p> <pre><code>LRESULT CALLBACK my_callback(HWND hwnd, UINT ui, WPARAM wp, LPARAM lp) { Window * self = reinterpret_cast&lt;Window*&gt;( GetWindowLongPtr(hwnd, 0)); return self-&gt;update(hwnd, ui, wp, lp); // handle it. } </code></pre> <p>Register that function as the wnd-proc and store the <code>this</code> pointer of your Window object using SetWindowLongPtr. In the <code>WNDCLASSEX</code> structure, there is a <code>cbWndExtra</code> field that you assign <code>sizeof(Window*)</code> to, which is enough storage to just contain the this pointer. Then you can call </p> <pre><code>SetWindowLongPtr(my_hwnd, 0, reinterpret_cast&lt;LONG_PTR&gt;(this)); </code></pre> <p>to put the this pointer into that region. Then receive it like the above and delegate to the real member function. You could in theory also use a static member function. But you have to be careful. Calling a static member function from C code can cause bugs, because the calling convention might be different between C code and C++ code. For Windows, that may not be an issue - I don't know. So better check yourself in addition.</p> <hr> <p>The thing you try is invalid. You try to return a pointer to the function call operator, but there is no object provided when the call is made, beside that your conversion operator has the wrong type. The type that the conversion operator returns has function pointer type, but not member function pointer type. The closest you can get to is to use the proper type:</p> <pre><code>struct adder; typedef int (adder::*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 &amp;adder::operator(); } }; </code></pre> <p>Now, your conversion operator is not even considered, because it has to be called like this:</p> <pre><code>adder a(10); cout &lt;&lt; (a.*(int_to_int)a)(2); // expected: 12 </code></pre> <p>And manually like this:</p> <pre><code>// note, we just get the member function pointer using the conversion operator. // Nothing is related to the actual temporary adder object. int_to_int i = adder(5); cout &lt;&lt; (adder(10).*i)(2); // expected: 12 </code></pre> <p>The usual function call syntax does not cope with that. In short, what you try is not possible. </p> <p>I think another question is, why the heck do you want to do that? I think we can help you better when we know what the original problem is. If you are trying to make it look and work like a function call, you don't need any conversion operators at all:</p> <pre><code>struct adder { int n_; adder (int n) : n_(n) {} int operator() (int x) { return x + n_; } }; adder a(10); cout &lt;&lt; a(2); // expected: 12 </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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