我试图在 .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/