我有一個棘手的語法錯誤,我找不出來。我想運行一個函數委託,其中上下文是一個指向對象的指針。語法錯誤:使用指向對象的指針調用成員函數指針
語法錯誤:
((object)->*(ptrToMember)) // the compiler doesn't like the ->*
Where
object
is of the typeComponent*
And
ptrToMember
is of the typevoid (Component::*EventCallback)()
下面是一個語法錯誤代碼:
typedef void (Component::*EventCallback)();
...
std::weak_ptr<Component> wc(mySharedPtr);
EventCallback ec = &Component::run;
((wc.lock())->*(ec))(); // syntax error
(wc.lock()->(*ec))(); // simpler version but still syntax error
// This is ok, but is there any significant time or memory involved in this reference object creation?
Component& wcc = *wc.lock();
(wcc.*ec)();
太懶了搜索這個的副本所以在這裏'(object - > * ptrToMember)()' –
哇,這麼多冗餘括號! –