Alex Rivera | Logout

C++. Class method pointers

Asked 2010-07-23T15:02:02.197
9

There is a class

class A {
public:
    A() {};

private:
    void func1(int) {};
    void func2(int) {};


};

I want to add a function pointer which will be set in constructor and points to func1 or func2.

So I can call this pointer (as class member) from every class procedure and set this pointer in constructor.

How can I do it?

Edit
Report

1 Answer

0

Some example...

class A; // forward declaration
typedef void (A::*func_type)(int);

class A { 
public:
  A() {
    func_ptr = &A::func1;
  }

  void test_call(int a) {
    (this->*func_ptr)(a);
  }

private:
  func_type func_ptr;

  void func1(int) {}
  void func2(int) {}
};
answered 2010-07-23T15:10:00.320

Your Answer