0

I would like to use and work with pointer to some member function and I also want to be able to call that (or other) member function back. Lets say, I have headers like this:

class A{
  public:
  template<class T> void Add(bool (T::*func)(), T* member){
    ((member)->*(func))();
  }
};
class B{
  public:
  bool Bfoo(){return 1;}
  void Bfoo2(){
    A a;
    a.Add(Bfoo, this);
  }
};

and cpp like this:

main(){
  B f;
  f.Bfoo2();
}

I've got following error:

main.h(22) : error C2784: 'void __thiscall A::Add(bool (__thiscall T::*)(void),T *)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'

I need to call A::Add from many classes (and send information about class method and its instance) so that's why I want to use templates

Using Microsoft Visual C++ 6.0. What am I doing wrong? I cannot use boost.

1
  • Is this even possible? Assigning a method to a normal function pointer? I would use the stuff around std::function (C++11 standard) for this. But you can't, as you are restricted to use a crappy compiler. Commented Feb 19, 2015 at 15:06

2 Answers 2

1

In my opinion, right way to do what you need is to use inheritance, for example:

class A {
  virtual void Add() = 0;
}

class B : public A {
  void Add() {...}
}

class C : public A {
  void Add() {...}
}

So in your main you can do:

A* a = new B();
a->Add(); /* this call B::Add() method */
Sign up to request clarification or add additional context in comments.

Comments

0

You need to pass an address of function

a.Add(&B::Bfoo, this);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.