如何在A类中使用函数指针调用B类中的函数?

来源:百度知道 编辑:UC知道 时间:2024/05/06 14:16:04
B类中我已经定义了三个PUBLIC成员函数,想在A类中通过使用函数指针调用这三个函数。

怎么写代码呢?

可以追加分数!!
您能不能把这个网页里的内容粘贴到我这个问题下面,因为我现在访问不了你说的这个网站,呵呵

typedef void (*FuncPtr)(void);

class A{
public:
FuncPtr ptrFunc1;
FuncPtr ptrFunc2;
FuncPtr ptrFunc3;
};

class B{
public:
//函数必须定义成静态的
static void Func1()
{ printf("Func1() in class B called.\n"); }
static void Func2()
{ printf("Func2() in class B called.\n"); }
static void Func3()
{ printf("Func3() in class B called.\n"); }
};

int main()
{
A AObject;//定义A类的对象
B BObject;//定义B类的对象

AObject.ptrFunc1 = (FuncPtr)BObject.Func1;
AObject.ptrFunc2 = (FuncPtr)BObject.Func2;
AObject.ptrFunc3 = (FuncPtr)BObject.Func3;

AObject.ptrFunc1();
AObject.ptrFunc2();
AObject.ptrFunc3();

getchar();
return 0;
}