Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy a callback?
    primarykey
    data
    text
    <p>Is it possible to copy the callback of one object into another?</p> <p>Right now I'm dealing with a data structure with a member like the following:</p> <pre><code>class Obj { // some unrelated variables // some unrelated functions void (*callback)(Obj&amp; r, void* user); void* callback_data; } </code></pre> <p>The callback is assigned like the following:</p> <pre><code>o-&gt;callback = AFunctionInAnotherScope; </code></pre> <p>A vector of <code>Obj</code> is kept but a <code>clear()</code> has to be invoked on that vector every iteration.</p> <p>The callback function is in another scope which also assigns the callback.</p> <p>The current scope has no access to the callback function but I'd like to create new Obj and assign them the same callback. Is there a way to do this?</p> <p>e: So far I've tried using <code>memcpy(&amp;copyObj, &amp;vectorOfObj.at(0), sizeof(Obj));</code> but I don't think I used that properly.</p> <p>Thanks in advance.</p> <p>Update: Here is what the file that creates the Obj looks like:</p> <p><strong>Caller.cpp</strong></p> <pre><code>#include "SomeClass.h" void Callback(Obj&amp; r, void* dummy_data) { // do things } int main( int argc, char* argv[] ) { for(int i = 0; i &lt; SomeClass::vectorOfOBj.size(); ++i) { vectorOfObj.at(i).callback = Callback; } } </code></pre> <p>Later on, in another file that doesn't have access to Caller.cpp but does have access to SomeClass.h</p> <pre><code>#define VAR(V,init) __typeof(init) V=(init) #define FOR_EACH(I,C) for(VAR(I,(C).begin());I!=(C).end();I++) void Run() { FOR_EACH(o, vectorOfObjects) { Obj &amp;b = *o; r-&gt;callback(b, o-&gt;callback_data); } vectorOfObjects.clear(); // repopulate vectorOfObject, might not be the same size as before. Could be bigger } </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. 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