2

I'm trying to assign function pointer to a variable (the variable located in the base class) in derived class constructor.

My Code:

class A {
 public:
  virtual void print()=0;
 protected:
  unsigned long (*func)(unsigned char *,int)
}
unsigned long A::f1(unsigned char *c,int a){...}
unsigned long A::f2(unsigned char *c,int a){...}

class B: public class A {...}
B::B(int which){
switch (which){
 case 1:
  func=f1; //first error
  break;
 case 2:
  func=f2; //second error
  break;
}

Error:

error: cannot convert 'A::f1' from type 'long unsigned int (A::)(unsigned char ,int)' to type long unsigned int ()(unsigned char *,int)'

error: cannot convert 'A::f2' from type 'long unsigned int (A::)(unsigned char ,int)' to type long unsigned int ()(unsigned char *,int)'

Community
  • 1
  • 1
Fleev
  • 77
  • 8
  • 3
    First repeat after me: A pointer to a *member* function is not a pointer to a *non-member* function (the error message should tell you that, it actually tells you the *correct* type). Then read about [`std::function`](http://en.cppreference.com/w/cpp/utility/functional/function) and [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind). – Some programmer dude Aug 18 '15 at 23:53

0 Answers0