Object method as callback

 

Hello,

I'm trying to use an object method as a callback, inside an object method.

typedef int (*IntCallback)();

class Obj
{
    public:
    Obj()
    {

    }

    public:
    int func()
    {
        return 1;
    }

    public:
    void print_icb(IntCallback icb)
    {
        Print(icb());
    }

    public:
    void print()
    {
        // Both not compiling : 'func' - pointer to this function type is not supported yet
        print_icb(func);
        print_icb(Obj::func);        
    }
};

The only solution i found was to take out 'func' of the class scope, but i want to keep the benefit of ... well ... the class scope.

Is it possible to achieve what i'm trying to do ? Is my syntax wrong somewhere ? How would you procede otherwise ?


Thanks for your advices.

 

You can't because it's not supported yet, but maybe you can come close with

interface IntCallback
  {
   int func();
  };
class Obj : public IntCallback
  {
public:
                     Obj()
     {

     }

public:
   int               func()
     {
      return 1;
     }

public:
   void              print_icb(IntCallback *icb)
     {
      Print(icb.func());
     }

public:
   void              print()
     {
      print_icb(&this);
     }
  };
 
 (dirty hack):
put the callback inside a struct or class

Please refer to:
 
Amir Yacoby #:

You can't because it's not supported yet, but maybe you can come close with

That's interesting, your callbacks have to be public then, not really clean but still functional

Soewono Effendi #:
 (dirty hack):
put the callback inside a struct or class

Please refer to:

Yes that's the best solution i found.

Reason: