C++存儲功能說你定義一個回調函數這樣:不用爭論
typedef std::function<void(float)> Callback;
而且你有一個功能,例如:
void ImAFunction(float a)
{
//Do something with a
}
有沒有一種辦法能夠存儲沒有參數的函數然後傳遞給它一個?
像這樣的:
//Define the Callback storage
Callback storage;
storage = std::bind(ImAFunction, this);
//Do some things
storage(5);
這是我的一些下面我真正的代碼解釋無法工作。
如果我將值與std :: bind函數綁定,我可以接近我想要的東西。如:
//Change
//storage = std::bind(ImAFunction, this);
storage = std::bind(ImAFunction, this, 5.0); //5.0 is a float passed
這個工作,但是當我去通過函數傳遞一個值的結果是什麼我將其設置爲前:
storage(100); //Output is still 5
我立足的事實,我認爲這是這篇文章可能。
http://www.cprogramming.com/tutorial/function-pointers.html
它不使用的功能或綁定功能,但是不會通過指針參數,並執行正是我需要的。我不只是跳過綁定函數的原因是因爲我試圖將該函數存儲在一個類(私有)中,並且如果它是模板是因爲它是用該類創建的,我無法存儲它。
上述製得的誤差來源於此代碼:
struct BindInfo {
Callback keyCallback;
int bindType;
bool isDown;
bool held;
std::string name;
};
template <class T1>
void bindEvent(int bindType, T1* keydownObj, void(T1::*keydownF)(float), std::string name)
{
BindInfo newKeyInfo = { std::bind(keydownF, keydownObj), bindType, false, false, name };
inputBindings.insert(std::pair<int, BindInfo>(BIND_NULL, newKeyInfo));
};
錯誤是:
No viable conversion from '__bind<void(Main::*&)(float), Main *&>' to 'Callback' (aka 'function<void (float)>'
這是可能的?提前致謝。
你可能會尋找'的std ::綁定(ImAFunction,這,的std ::佔位符:: _ 1)' – 2014-10-09 02:49:25