c++ - 将成员函数作为参数传递

标签 c++ pointers

假设我在类中有这个公共(public)方法:

uli returnSum()
{
    for_each(i, j, doSum);
    return this->sum;
}

void doSum(short int y)是同一类的私有(private)方法。我如何将它作为参数传递给 for_each?

使用上面的语法我得到 error: must use .* or ->* to call pointer-to-member function in __f (...)'this->*doSum都不是工作。

我读到了一些关于创建指向该成员函数的指针并将其作为参数传递的内容,但我不太确定如何做到这一点。

最佳答案

你可以使用 std::bind,像这样

std::for_each(i, j, std::bind(&MyClass::doSum, this));

关于c++ - 将成员函数作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14738436/

相关文章:

C. 正确处理指向函数的双指针,该函数分配一个结构并返回指向它的指针——在不同的函数中读取、显示、释放

c++ - 正确翻转/镜像图像像素?

c++ - 提升条件互斥体性能不佳

c++ - 在 C++ 中删除我所有的 Lua 用户数据

c++ - 指针内联的意思?

c++ - 指针数组的动态初始化

C++ 标准 : dereferencing NULL pointer to get a reference?

c++ - 如何在某个位置1或0设置二进制形式的数字

c++ - QThread中的QTcpSocket将commitTransaction但是当调用Write时 "Cannot create children for a parent that is in a different thread."

c++ - 为什么在 NULL 检查后会在指针上调用递归函数?