2017-08-24 7 views
1

ブーストでバインドメンバー関数が必要な理由ブースト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 

我々は& 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暗黙の変換のためオプションです。

関数へのポインタは、非メンバ関数または静的メンバ関数のアドレスで初期化できます。関数対ポインタ暗黙変換のため、アドレス演算子はオプションです。

関連する問題