2011-11-22 61 views
0

我有一個類實現如下。在構造函數中,我收到一個編譯錯誤。請你告訴我爲什麼?成員的函數指針錯誤

class A{ 

public: 
    typedef void (A::*HANDLER)(); 
    void test1(){ 
     printf("This is test 1"); 
    } 
    void test2(){ 
     printf("This is test 2"); 
    } 

    A(){ 
     HANDLER h= &A::test1; 
     h(); // an error spawn here with the description: term does not evaluate to a function taking 0 arguments 
    } 
}; 

回答

0

您應該使用,成員指針操作->*這樣:

(this->*h)(); 

Online Demo您的代碼示例的

0

g++錯誤消息是更多的信息:

bar.cc: In constructor 'A::A()': 
bar.cc:15:11: error: must use '.*' or '->*' to call pointer-to-member function in 'h (...)', e.g. '(... ->* h) (...)' 

事實上,如果你改變你的電話到(this->*h)();,它通過編譯器。