mardi 2 février 2016

How to make a real functor and could be called in callback context?

I'm try to learn to make function object. That is which could be called in a callback context. But I failured.

  6 class Functor {
  7     public:
 10         typedef void(*fp)(int);
 11
 13         operator fp() const {
 14             cout
 15                 << "convertor be called"
 16                 << endl;
 17         }
 18
 19         // maybe, this function will be called
 20         void operator()(int) {
 21             cout
 22                 << "functor be called"
 23                 << endl;
 24         }
 25
 26 };
 27
 28
 29 void caller(Functor::fp f) {
 30     f(1);
 31 }
 33
 34 int main(int argc, char** argv) {
 35
 36     Functor f;
 37     
        //error
        caller(f);

        //error
        Functor::fp p = nullptr;
        p=f;
        caller(p);

 38     return 0;
 39   }

The above two way, pass by a direct Functor object or assgin it to a pointer all failured.

Aucun commentaire:

Enregistrer un commentaire