目前我已经创建了一个名为A的类,它创建了一个指向函数的指针数组,并声明了函数的原型,请查看下面的A.h.
#ifndef A_H
#define A_H
class A
{
public:
//Function Definitions that will be called from the seq function below.
A::A()
//prototype functions that will be pointed to from array
void f1(int);
void f2(int);
void f3(int);
void (*funcs[3])(int);
//Destructor method
virtual ~A();
};
然后我在下面定义了A.cpp,其函数定义为f1,f2,f3。
A::A()
{
}
void A::f1(int a){ cout << "This is f1: " << endl; }
void A::f2(int b){ cout << "This is f2: " << endl; }
void A::f3(int c){ cout << "This is f3: " << endl; }
//Would this be valid?
funcs[0] = f1;
funcs[1] = f2;
funcs[2] = f3;
A::~A()
{
//dtor
}
我如何从main.cpp
访问该数组include "A.h"
.
.
.
int main()
{
//This doesn't work, what is the proper way to access the pointers to functions?
A a;
a.funcs[0](1);
}
应打印出来&#34;这是f1:&#34;。这确实困扰着我,我一直在阅读不同的线程,甚至使用typedef但无济于事。真的很感激任何帮助。如果您需要更多详细信息,请告知。