我有一個Visual Studio 2008 C++ 03項目,我想使用boost::function
對象來設置指針的值。事情是這樣的:將賦值運算符綁定到boost :: function對象
boost::function< void(int*) > SetValue;
boost::function< int*() > GetValue;
int* my_value_;
SetValue = boost::bind(my_value_, _1); // how should this look?
GetValue = boost::bind(my_value_); // and this?
int v;
SetValue(&v);
assert(my_value_ == &v);
int* t = GetValue();
assert(t == my_value_);
有沒有辦法做到這一點還是需要像一箇中間功能:
void DoSetValue(int* s, int* v) { s = v; };
SetValue = boost::bind(DoSetValue, my_value_, _1);
感謝
boost :: phoenix看起來可能是一個很好的解決方案,但是我很難從文檔中瞭解如何在這種情況下使用它。 例如:'SetValue = boost :: phoenix :: val(my_value_);'在稍後調用SetValue()時似乎不執行任何操作。 – PaulH
嘗試類似'boost :: phoenix :: val(my_value_)= phoenix_placeholder :: _ 1' –
是的,但是我如何獲得函數指針'SetValue'? – PaulH