c++ - 成员函数指针的静态成员数组

标签 c++ arrays function pointers static


我试图在 .cpp 文件中定义一个属性,它应该是指向名为 Hand 的类的成员函数的指针数组。
数组和函数都是Hand的成员并且数组是静态的(如果不应该请纠正我)。
这是我达到的结果:

static bool Hand::*(Hand::hfunctions)[] ()=
{&Hand::has_sflush,&Hand::has_poker,&Hand::has_full,&Hand::has_flush,
&Hand::has_straight,&Hand::has_trio,&Hand::has_2pair,&Hand::has_pair};                                              


我收到此错误:hand.cpp:96:42: 错误:将“hfunctions”声明为函数数组。
我想类型定义是错误的,所以我需要知道如何才能使定义正确

最佳答案

语法比较复杂:

class Hand
{
    bool has_sflush();
    static bool (Hand::*hfunctions[])();
    ...
};

bool (Hand::*Hand::hfunctions[])() = {&Hand::has_sflush, ...};

实现此目的的一种方法是逐渐增加复杂性,使用 cdecl.org在每一步检查自己:

int (*hFunctions)()

declare hFunctions as pointer to function returning int


int (Hand::*hFunctions)()

declare hFunctions as pointer to member of class Hand function returning int

Warning: Unsupported in C -- 'pointer to member of class'


int (Hand::*hFunctions[])()

declare hFunctions as array of pointer to member of class Hand function returning int

Warning: Unsupported in C -- 'pointer to member of class'


现在将 int 替换为 bool(遗憾的是,cdecl.org 不理解 bool);所以你得到了声明的语法。

对于定义,将 hFunctions 替换为 Hand::hFunctions,并像您一样添加初始化部分。

关于c++ - 成员函数指针的静态成员数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17821508/

相关文章:

c++ - 如何编译头文件、实现、驱动文件

javascript - 如何展开和折叠单个字符数组?

c# - IndexOutOfRangeException 由于一些奇怪的原因

c# - 如何将值从结构放置到数组?

r - 如何在R函数中将对象导出到并行集群

javascript - 去抖一个方法,但继续合并和内存参数

c++ - 无法计算字符类型的数量

c++ - 函数使用对象,对象使用函数

c++ - 如何将 char* 分配给 char 数组?

c++ - 我如何自动递增每个类对象?