Note that there are some explanatory texts on larger screens.

plurals
  1. POCallback to a non-static C++ Member Function
    primarykey
    data
    text
    <p>I would like someone to shed some light this code snippet, which confuses me.</p> <pre><code> //------------------------------------------------------------------------------- // 3.5 Example B: Callback to member function using a global variable // Task: The function 'DoItB' does something that implies a callback to // the member function 'Display'. Therefore the wrapper-function // 'Wrapper_To_Call_Display is used. #include &lt;iostream.h&gt; // due to: cout void* pt2Object; // global variable which points to an arbitrary object class TClassB { public: void Display(const char* text) { cout &lt;&lt; text &lt;&lt; endl; }; static void Wrapper_To_Call_Display(char* text); /* more of TClassB */ }; // static wrapper-function to be able to callback the member function Display() void TClassB::Wrapper_To_Call_Display(char* string) { // explicitly cast global variable &lt;pt2Object&gt; to a pointer to TClassB // warning: &lt;pt2Object&gt; MUST point to an appropriate object! TClassB* mySelf = (TClassB*) pt2Object; // call member mySelf-&gt;Display(string); } // function does something that implies a callback // note: of course this function can also be a member function void DoItB(void (*pt2Function)(char* text)) { /* do something */ pt2Function("hi, i'm calling back using a global ;-)"); // make callback } // execute example code void Callback_Using_Global() { // 1. instantiate object of TClassB TClassB objB; // 2. assign global variable which is used in the static wrapper function // important: never forget to do this!! pt2Object = (void*) &amp;objB; // 3. call 'DoItB' for &lt;objB&gt; DoItB(TClassB::Wrapper_To_Call_Display); } </code></pre> <p><strong>Question 1:</strong> Regarding this function call:</p> <pre><code>DoItB(TClassB::Wrapper_To_Call_Display) </code></pre> <p>Why does <code>Wrapper_To_Call_Display</code> not take any arguments, although it is supposed to take a <code>char*</code> argument according to its declaration?</p> <p><strong>Question 2:</strong> <code>DoItB</code> is declared as</p> <pre><code>void DoItB(void (*pt2Function)(char* text)) </code></pre> <p>What I’ve understood so far is that <code>DoItB</code> takes a function pointer as argument, but why does the function call <code>DoItB(TClassB::Wrapper_To_Call_Display)</code> take <code>TClassB::Wrapper_To_Call_Display</code> as argument even tough it’s not a pointer?</p> <p>Thanx in advance</p> <p>Source of code snippet: <a href="http://www.newty.de/fpt/callback.html" rel="nofollow">http://www.newty.de/fpt/callback.html</a></p>
    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. 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