array of class member functions

Can someone help me?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

class a
{
      public:
             void function(){ cout << "hi" << endl; }
void functionTimesTwo(){ cout << "hello" << endl; }
void functionDivideByTwo(){ cout << "hallo" << endl; }
      };
typedef void a::(*IntFunctionWithOneParameter) ();

int main()
{
    a obj;
    IntFunctionWithOneParameter functions[] = 
    {
        obj.function, 
        obj.functionTimesTwo, 
        obj.functionDivideByTwo
    };

    for(int i = 0; i < 3; ++i)
    {
        functions[i]();
    }
    system("PAUSE");
}


/*
This is part of my project, i am just running a sample problem so i get the right syntax
ERRORS
11 C:\Users\minahnoona\Desktop\hgf.cpp typedef name may not be a nested-name-specifier 
11 C:\Users\minahnoona\Desktop\hgf.cpp typedef `<anonymous>' is initialized (use __typeof__ instead) 
11 C:\Users\minahnoona\Desktop\hgf.cpp confused by earlier errors, bailing out 
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdio>
#include <functional>
#include <array>

struct a 
{
    void f() const { std::puts("a::f"); }
    void g() const { std::puts("a::g"); } 
};

int main()
{
  a my_a;    
    
  std::array functions 
    { std::bind(&a::f, my_a), 
      std::bind(&a::g, my_a) };
      
  for (auto const& fn : functions) fn();
}


Live demo:
http://coliru.stacked-crooked.com/a/ea6ded7c6ecebb3d

As much as I dislike bind expressions, they may remain the best choice here.
Last edited on
Topic archived. No new replies allowed.