2017-08-24 33 views
1

在升压DOC:为什么我们需要在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; 

以这种方式,我们不使用&。

回答

4

运算符地址(即&)是获取指向成员函数的指针的必须条件。对于non-member function,它是可选的,因为function-to-pointer隐式转换。

指向函数的指针可以用非成员函数或静态成员函数的地址初始化。由于函数到指针的隐式转换,操作符的地址是可选的:

相关问题