简体   繁体   中英

Why we need & in bind member function in boost?

In boost doc:

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

Why we need &xyz::foo, not just xyz::foo?

int f(int a, int b)
{
    return a + b;
}
std::cout << bind(f, 1, 2)() << std::endl;  

In this way, we don't use &.

The address-of operator (ie & ) is obligatory to get a pointer to member function. For non-member function it's optional because of function-to-pointer implicit conversion.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM