C++: Magic member callbacks

Short post about C++ this time – though calling it a request would be more appropiate. I’m trying to create some kind of magic callback to do this:

class Caller {
	Callback c;
	Caller(Callback c) : c(c) {}
	void doit(){ c(this); }

Shouldn’t be too difficult, right? There are some hidden complexities, of course, mostly regarding the callback parameter type, but the idea is simple, keep the caller dependant only in the callback, not in the callee.

Templates are not a valid solution as the callee may have more than one callback (i.e. expect more than a single object to finish and call the callback) so the whole idea of this is having the callback “bind” to a member method when created, doesn’t matter which one.

I have a solution, tough I’m not too happy about it for now. I’ll post it next week, unless someone comes up with a better idea (you know how to submit it if you do, right?).


2 Comments on “C++: Magic member callbacks”

  1. Leonardo Secotaro says:

    /*This is my implementation*/

    class CallBack {
    /* … */
    virtual void operator() (void*) = 0;

    #include “CallBack.h”
    #include “Cain.h”
    #include “Abel.h”

    class Objeto: public CallBack{
    /* … */
    void Execute(){
    Abel *pObj = new Abel( this );
    Cain *pObj2 = new Cain( this );



    delete pObj;
    delete pObj2;

    void Doit_Abel(Abel* Who){

    /* … */

    printf(“I am a good Kidn”);

    void Doit_Cain(Cain* Who){

    /* … */

    printf(“I am a killern”);

    void operator() (void* Who) {
    Adan *pFather = (Adan*) Who;

    if (pFather->WhoamI() == CAIN )
    Doit_Cain( (Cain*)Who);
    else if (pFather->WhoamI() == ABEL )
    Doit_Abel( (Abel*)Who);

    enum Family{CAIN,ABEL};

    class Adan{
    virtual Family WhoamI()=0;

    #include “CallBack.h”
    #include “Adan.h”

    class Abel: public Adan{
    /* … */
    CallBack *_c;
    Abel(CallBack *c): _c(c){}

    Family WhoamI(){ return ABEL;}

    void execute(){

    #include “CallBack.h”

    class Cain: public Adan{
    /* … */
    CallBack *_c;
    Cain(CallBack *c): _c(c){}

    Family WhoamI(){ return CAIN;}

    void execute(){


    int main(void) {

    Objeto *pObjeto = new Objeto();


    delete pObjeto;

    return EXIT_SUCCESS;

    I am a good Kid
    I am a killer


  2. nico says:

    It does work but you’re missing part of the “spec”, you can’t choose a callback function (i.e. you’re tied to a single callback or a type overloaded callback).
    Other than that, the code is quite close to what I’ve been looking for. Check the solution back in a couple of days (still working on it!)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s