a repeated handling with different methods calls but same parameters

I have several functions doing similar things, inside their implementations, the parameters are the same, but they call different methods.

I want to create one help function to make the structure easier, and reduce the duplication. I heard template might be one solution. But I am not sure how to use it in this case.

Can anyone help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  void GetA(...XXX)
  {
     for()
     {
        XXX->CalA();
     }
  }
 
  void GetB(...XXX)
  {
     for()
     {
        XXX->CalB();
     }
  }

  void GetC(...XXX)
  {
     for()
     {
        XXX->CalC();
     }
  }
If you had a pointer to member function, you could move that stuff into a common helper function and pass the function you wanted run.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
helper(... obj, func)
{
    for()
        obj->func();
}

void GetA(... XXX)
{
     helper(XXX, CalA);
}

void GetB(... XXX)
{
     helper(XXX, CalB);
}

void GetC(... XXX)
{
     helper(XXX, CalC);
}


The syntax for pointers to member functions suck.
http://www.parashift.com/c++-faq/pointers-to-members.html
Last edited on
Topic archived. No new replies allowed.