This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




7年前关闭。




有人可以帮我确定我要去哪里错吗?我正在尝试使用指向基类函数的函数指针

错误C2064:术语不等于行号30上带有0个参数的函数的值,即*(app)()
#include<stdio.h>
#include<conio.h>
#include<stdarg.h>
#include<typeinfo>

using namespace std;

class A{
public:
    int a(){
        printf("A");
        return 0;
    }
};

class B : public A{
public:
    int b(){
        printf("B");
        return 0;
    }
};

class C : public B{
public:
    int(C::*app)();
    int c(){
        app =&C::a;
        printf("%s",typeid(app).name());
        *(app)();
        printf("C");
        return 0;
    }
};
int main(){
    C *obj = new C();
    obj->c();
    getch();
}

最佳答案

在调用指向成员函数的指针时,必须使用。*或-> *

    class C : public B{
    public:
        int(C::*app)();
        int c(){
            app =&C::a;
            printf("%s",typeid(app).name());
            (this->*app)(); // use ->* operator within ()
            printf("C");
            return 0;
        }
    };

关于c++ - 指向基类错误的成员函数的指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15804586/

10-12 22:00