samedi 1 septembre 2018

How can I pass a member function from a derived class as a callback?

I have a simple class X

class X {
public:
    template<typename T>
    void doSomething(T &completion) {
        std::cout << completion(10) << std::endl;
    }
};

and a class A and B

class A {
public: 
 // some code
 C* c;
};

class B : public A {
public:
  int test(int x) {
    return x * x;
  }

  void execute() {
   auto lambda = [] (int x) { cout << x * 50 << endl; return x * 100; };
   c->test(lambda); // works
   c->test(&B::test); // does not work
  }
};

I want to pass to the doSomething method a member method of class B (or any other class derived from A) but it just does not work :/

Aucun commentaire:

Enregistrer un commentaire