jeudi 14 juin 2018

Passing method as templated argument C++11

I have this structure:

class A {
  protected:
     int setI(int i){_i = i;}
  private:
     int _i;
}
class B {
  protected:
     int setZ(int z){_z = z;}
  private:
     int _z;
}
class C : public A, public B {};

Then I want to call methods setI or setZ from templated function elsewhere

Something like:

template <class P>
void myMethod(P &myclass, void (P::*setter) (const int)) const
   {
         int var = 9;
         (myclass.*setter)(var);
   }

I use this method like this:

   C c;
   mymethod(c, &C::setI); 

It fails because setI is defined in A not in C, is there any way to pass method from class A or class B following this structure?

Thanks and regards

Aucun commentaire:

Enregistrer un commentaire