我有成員函數類:的std ::一個成員函數綁定到對象指針
typedef std::function<bool (const std::string &)> InsertFunction;
bool insertSourceFile(const std::string &);
bool insertSourceDir(const std::string &);
bool insertHeaderFile(const std::string &);
bool insertHeaderDir(const std::string &);
我想有這些InsertFunction
S IN另一個類中的一個的參考,並用它來做好自己的工作(不必爲這些功能中的每一個做類)。我嘗試使用類構造函數和std::bind
的成員函數的隱式第一個參數綁定到對象的問題指針:
new ListParser(p_target->sourceDirs(), bind(&Target::insertSourceFile, p_target, _1), this);
與
ListParser::ListParser(const stringSet &dirs, const InsertFunction &insert,
State* parent = 0)
: ParserState(parent),
m_dirs(dirs),
m_insert(insert)
{ }
UPDATE:感謝@lijie時,現在編譯源程序,但是當函數m_insert被調用時,std::bad_function_call
被拋出std::exception
。什麼可能是錯的?謝謝!如果您需要更多信息,請詢問!
謝謝,我用更正的語法更新了這個問題。我確實希望使用m_insert作爲一個函數,它使用`const std :: string&`並返回一個`bool`,並且該函數是`p_target`(因此是綁定)中的一個。 – rubenvb 2010-11-23 15:58:26