繁体   English   中英

成员函数指针错误

[英]Member's function pointer error

我有一个实现如下的类。 在构造函数中,出现编译错误。 你们能告诉我为什么吗?

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
    }
};

您应该以这种方式使用指向成员运算符->*指针:

(this->*h)(); 

您的代码示例的在线演示

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)(); ,它将通过编译器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM