在 boost 文档中:
Binding member functions can be done similarly. A bound member function takes in a pointer or reference to an object as the first argument. For instance, given:
struct xyz
{
void foo(int) const;
};
xyz's foo member function can be bound as:
bind(&xyz::foo, obj, arg1) // obj is an xyz object
为什么我们需要 &xyz::foo,而不仅仅是 xyz::foo?
int f(int a, int b)
{
return a + b;
}
std::cout << bind(f, 1, 2)() << std::endl;
这样,我们就不用&了。
最佳答案
address-of 运算符(即 &
)是获取指向成员函数的指针所必需的。对于 non-member function它是可选的,因为 function-to-pointer隐式转换。
A pointer to function can be initialized with an address of a non-member function or a static member function. Because of the function-to-pointer implicit conversion, the address-of operator is optional:
关于c++ - 为什么我们需要&在boost中绑定(bind)成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45852416/