简体   繁体   English

使用std :: function调用对象列表上的任何对象成员函数

[英]Call any object member function on a list of objects using std::function

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. 我用std :: function尝试了一下,但最终无法正常工作。 Any suggestions? 有什么建议么? (I'm back to C++ after many years of C#, so forgot a lot). (经过多年的C#工作后,我回到了C ++,所以忘记了很多)。

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);
    }
}
void CMyService::ApplyToList(void (CMyListItem::*func)(double), double myValue) {
    for (auto p : myList) {
        (p->*func)(myValue);
    }
}

With pre-C++11 compilers: 使用C ++ 11之前的编译器:

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);
  }
}

You could use lambdas 您可以使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在成员初始化列表中使用std :: function - Using std::function in member initialization list 当对象绑定到成员函数时,为什么std :: function会调用析构函数? - Why will std::function call destructor when an object was bound to a member function? 对 std::function object 的调用不匹配,它是指向成员 function 的指针 - No match for call to std::function object which is pointer to member function 没有对象就无法调用成员函数std :: string class :: function() - Cannot call member function std::string class::function() without object c ++ std :: sort()由任何可比较的成员使用一个比较函数构成的对象向量 - c++ std::sort() a vector of objects by any comparable member using only one compare function 使用模板时使用std :: function调用成员函数 - Using std::function to call a member function while using templates 成员函数的std :: async调用 - std::async call of member function 有什么方法可以检查std :: function是否指向有效对象的成员? - Is there any way to check if an std::function points to a member of a valid object? 创建 std::functional object 来包装 lambda 并调用 static 成员 function - creating std::functional object to wrap a lambda with a call to static member function 插入时调用 std::map 值对象的成员函数 - Call member function of std::map value object on insertion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM