2011-02-01 56 views
2

假設我有方法:的boost ::綁定和參考變量temp

void foo(const std::string& s); 

我可以創建的boost ::功能:

boost::function<void(const std::string&)> f = boost::bind(foo, temp); 

,其中溫度是char *是f之前刪除是調用。

回答

5

是的。綁定不能知道char *可以被保存在一個字符串中,或​​者它被傳遞給一個字符串。爲了規避這種情況,請使用:

boost::bind(foo, std::string(temp)); 

以便您的temp作爲字符串複製到活頁夾中。

0

這是爲你編譯?它應該是

boost::function<void()> f = boost::bind(foo, std::string(temp)); 
+0

因此,爲什麼我一直被投票?我的回答是正確的。 – 2011-11-07 13:51:01