#include <iostream>
#include <vector>
#include <string>
#include <ostream>
#include <algorithm>
#include <boost/function.hpp>
using namespace std;
class some_class
{
public:
void do_stuff(int i) const
{
cout << "some_class i: " << i << endl;
}
};
class other_class
{
public:
void operator()(int i) const
{
cout << "other_class i: " << i << endl;
}
};
int main() {
// CASE ONE
boost::function<void (some_class, int) > f;
// initilize f with a member function of some_class
f = &some_class::do_stuff;
// pass an instance of some_class in order to access class member
f(some_class(), 5);
// CASE TWO
boost::function<void (int) > f2;
// initialize f2 with a function object of other_class
f2 = other_class();
// Note: directly call the operator member function without
// providing an instance of other_class
f2(10);
}
// output
~/Documents/C++/boost $ ./p327
some_class i: 5
other_class i: 10
質問>我々は::機能ブーストを通じて関数オブジェクトを呼び出すときに、なぜ我々は、インスタンスを提供する必要はありません。このクラスメンバ関数を呼び出すためのクラス?ブースト関数オブジェクトを呼び出すときに何のクラスインスタンスを必要としないのはなぜ::機能
私たちは、このような情報を次の行から提供しているからですか?
f2 = other_class();
コピーを作成します。インプレースで構築されたオブジェクトは、ステートメントの終わりに無効な値の一時的な値です。 – bdonlan
@bdonlanありがとう、私は私の答えを更新しました。 boost :: functionが実装されているかどうかはわかりませんでしたが、正しいですが、C++は基本的にそれを他の方法で許可していません。 – hvd