【问题标题】:Member function pointer array成员函数指针数组
【发布时间】:2016-12-07 18:34:23
【问题描述】:

在 A.h 中:

class A 
{
    typedef void (A::*fp)();
    void test1();
    void test2();
    A(int a);
}

在 A.cpp 我有:

A::A(int a)
{
    fp fpointers[] = {&A::test1,&A::test2};

    fpointers[a]();
}
void A::test1()
{
//do something
}
void A::test2()
{
//do something
}

这是我所拥有的简化和简化版本。但问题仍然有效。根据我的拙见,“fpointers”是您定义和声明函数指针数组的方式。我遇到fpointers[a]();行有问题我收到此错误:

明显调用括号前的表达式必须有 (pointer-to-) 函数类型

我做错了什么?

【问题讨论】:

标签: c++ arrays visual-studio pointers


【解决方案1】:

语法是

(this->*fpointers[a])();

如你所愿。

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    相关资源
    最近更新 更多