0

I have a list of objects where I need to call a member function - no big deal so far: iterate through the list, make that call - done.

Now I have various places that almost do the same on the same list, except that the called member function changes (argument is always the same, a double value).

I tried around a bit with std::function, but in the end can't get it working. Any suggestions? (I'm back to C++ after many years of C#, so forgot a lot).

This is how it looks now:

void CMyService::DoSomeListThingy(double myValue)
{
    for (std::list<CMyListItem*>::iterator v_Iter = myList.begin(); v_Iter != myList.end(); ++v_Iter)
    {
        (*v_Iter)->MethodToBeCalled(myValue);
    }
}

void CMyService::DoSomeThingDifferent(double myValue)
{
    for (std::list<CMyListItem*>::iterator v_Iter = myList.begin(); v_Iter != myList.end(); ++v_Iter)
    {
        (*v_Iter)->CallTheOtherMethod(myValue);
    }
}

And this is how I'd prefer it:

void CMyService::DoSomeListThingy(double myValue)
{
    ApplyToList(&CMyListItem::MethodToBeCalled, myValue);
}

void CMyService::DoSomeThingDifferent(double myValue)
{
    ApplyToList(&CMyListItem::CallTheOtherMethod, myValue);
}

void CMyService::ApplyToList(std::function<void(double)> func, double myValue)
{
    for (std::list<CMyListItem*>::iterator v_Iter = myList.begin(); v_Iter != myList.end(); ++v_Iter)
    {
        (*v_Iter)->func(myValue);
    }
}
Quentin
  • 62,093
  • 7
  • 131
  • 191
Robin Holenweger
  • 321
  • 3
  • 14

2 Answers2

3
void CMyService::ApplyToList(void (CMyListItem::*func)(double), double myValue) {
    for (auto p : myList) {
        (p->*func)(myValue);
    }
}

With pre-C++11 compilers:

void CMyService::ApplyToList(void (CMyListItem::*func)(double), double myValue) {
  for (std::list<CMyListItem*>::iterator v_Iter = myList.begin();
       v_Iter != myList.end(); ++v_Iter) {
    ((*v_Iter)->*func)(myValue);
  }
}
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
0

You could use lambdas

void CMyService::ApplyToList(std::function<void(CMyListItem*, double)> func, double myValue))
{
    for (std::list<CMyListItem*>::iterator v_Iter = myList.begin(); v_Iter != myList.end(); ++v_Iter)
    {
        func(*v_Iter, myValue);
    }
}

and

double val;
std::function<void(A*,double)> fun1 = [=](A *th,double) {
                                                  th->foo(val);
                                                 };

std::function<void(A*,double)> fun2 = [=](A *th,double) {
                                                  th->bar(val);
                                                 };
ApplyToList(fun1, val);
ApplyToList(fun2, val);
Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34