【发布时间】:2019-09-30 20:41:24
【问题描述】:
如果不使用std::bind,我就是找不到调用类方法的方法,因为我需要在tryfoo中使用参数调用这个方法。
//simple function
uint16_t getreg(const uint8_t& num)
{
return 0;
}
假设在 ClassItem 类中我们有公共方法
uint16_t ClassItem::getregI(const uint8_t &f)
{
return 1;
}
带有可调用函数的函数
void tryfoo (const uint8_t ¶m, std::function<uint16_t (const uint8_t&)> f)
{
// let's suppose that param= got some other value here
uint16_t result = f(param);
}
void basefunction(ClassItem &A)
{
tryfoo (0, getreg); // Why it's OK
tryfoo (0, A.getregI) // And this's NOT ?
}
【问题讨论】:
-
这不行,因为 C++ 设计者是这样设计的。
-
std::bind有什么问题? -
是的,我不明白问题是什么。 OP,您是否只是在问为什么语法
A.getregI不等于std::bind? -
@Acorn 我误解了 std::bind 的工作方式,但现在一切都变得清晰了。
标签: c++ class methods std-function