0

I want to make a template class whose constructor takes an object pointer and a pointer to one of the object's methods. The code below doesn't compile. What's the right syntax?

template <class Obj, typename Method>
class Foo
{
public:
  Foo(Obj *obj, Method method)
    :mObj(obj), mMethod(method)
  {}

  void callMethod()
  {
    mObj->mMethod();
  }

private:
  Obj* mObj;
  Method* mMethod;
};

class Bar
{
public:
  void method() {}
};

I get this error on the Foo constructor:

error C2440: 'initializing': cannot convert 
from 'void (__thiscall Bar::* )(void)'
  to 'void (__thiscall Bar::* *)(void)'
Mat
  • 202,337
  • 40
  • 393
  • 406
T Scherer
  • 401
  • 7
  • 14
  • 2
    pleae provide a [mcve]. The code you posted does not produce that error: https://ideone.com/e.js/hPEQ3J. There is one mistake in your code, but the error is about another one in code you didnt post – 463035818_is_not_an_ai Apr 22 '20 at 14:50
  • oh I have to correct myself. There are two mistakes visible in the code you posted, and the one from the error is also there, but nevertheless this is just a template and as such it does not produce that error – 463035818_is_not_an_ai Apr 22 '20 at 14:55

0 Answers0