Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to call member function without .* or ->* operator
    primarykey
    data
    text
    <p>Below method of calling <code>D::foo</code> function via pointer-to-member function will generate error: <strong>must use <code>.*</code> or <code>-&gt;*</code> to call pointer-to-member function in 'f (...)'</strong> .. of course that is not how we call pointer-to-member functions.</p> <p>The correct way of calling is <code>(d.*f)(5);</code> OR <code>(p-&gt;*f)(5);</code></p> <p>My question is, 'Is there a way to call member function of a class without the class object on left hand side? I wonder if we could pass class object (<code>this</code>) as regular argument?</p> <p>In my mind, at end of the day (at assembly/binary level) all member functions of a class are normal functions which should operate on n + 1 arguments where (+1 is for <code>this</code>) </p> <p>If we talk about <code>D::foo</code> function below, at assembly/binary level it should operate on two arguments:</p> <ol> <li>The class object itself (pointer to class D object called <code>this</code>) </li> <li>and the <code>int</code>.</li> </ol> <p>so, is there a way (or hack) to call <code>D::foo</code> with class object passed to it as function argument instead of using <code>. or -&gt; or .* or -&gt;*</code> operators on class object?</p> <p><strong>Sample Code:</strong></p> <pre><code>#include &lt;iostream&gt; using namespace std; class D { public: void foo ( int a ) { cout &lt;&lt; "D" &lt;&lt; endl; } int data; }; //typedef void __cdecl ( D::* Func)(int); typedef void ( D::* Func)(int); int main ( void ) { D d; Func f = &amp;D::foo; f(&amp;d, 5); return 1; } </code></pre> <p>One method is using boost bind i.e</p> <pre><code>(boost:: bind (&amp;D::foo, &amp;d, 5)) (); </code></pre> <p>EDIT: "Please note I am not looking for a version of this program which works, I know how to make it work"</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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