简体   繁体   English

指向成员函数的静态成员数组

[英]Static member array of pointers to member functions


I am trying to define in a .cpp file an attribute which should be an array of pointers to member functions of a class named Hand. 我正在尝试在.cpp文件中定义一个属性,该属性应该是指向名为Hand的类的成员函数的指针的数组。
Both the array and the functions are members of Hand and the array is static(please correct me if it should not). 数组和函数都是Hand的成员,并且数组是静态的(如果不需要,请更正我)。
This is what I reached: 这是我达到的目标:

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};                                              


I get this error: hand.cpp:96:42: error: declaration of 'hfunctions' as array of functions. 我收到此错误:hand.cpp:96:42:错误:将“ hfunctions”声明为函数数组。
I guess the type definition is worng so I need to know how can I make the definition right 我想类型定义很破旧,所以我需要知道如何正确定义

The syntax is a rather convoluted one: 语法很复杂:

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

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

A way to get to this is by gradually increasing complexity, using cdecl.org to check yourself at each step: 一种解决方法是逐渐增加复杂性,使用cdecl.org在每一步进行检查:

int (*hFunctions)()

declare hFunctions as pointer to function returning int 将hFunctions声明为返回int的函数的指针


int (Hand::*hFunctions)()

declare hFunctions as pointer to member of class Hand function returning int 将hFunctions声明为指向int类的Hand函数的成员的指针

Warning: Unsupported in C -- 'pointer to member of class' 警告:C语言不支持-“指向类成员的指针”


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

declare hFunctions as array of pointer to member of class Hand function returning int 将hFunctions声明为指向返回int的Hand函数类成员的指针的数组

Warning: Unsupported in C -- 'pointer to member of class' 警告:C语言不支持-“指向类成员的指针”


Now replace int by bool (sadly, cdecl.org doesn't understand bool ); 现在用bool替换int (不幸的是,cdecl.org不理解bool ); so you get the syntax of the declaration. 这样就得到了声明的语法。

For the definition, replace hFunctions by Hand::hFunctions, and add the initialization part, like you did. 对于定义,用Hand :: hFunctions替换hFunctions ,然后像您一样添加初始化部分。

Both the array and the functions are members of Hand and the array is static(please correct me if it should not). 数组和函数都是Hand的成员,并且数组是静态的(如果不需要,请更正我)。

If I understand correctly what you are asking, you should not . 如果我正确理解您的要求, 则不应该 You should abstract the operation as a base class, specialize it and hold the array as an array of pointers to the base class: 您应该将操作抽象为一个基类,对其进行专门化处理,并将该数组作为指向该基类的指针的数组保存:

struct Match // need a better name
{
     virtual bool matches() = 0;
     virtual ~Match() = default;
};

struct MatchSFlush: public Match { ... };

class Hand
{
    static std::vector<std::unique_ptr<Match>> matches;
};

If you have non static member functions with no arguments and returning bool you should write something like 如果您有带参数的非静态成员函数并返回bool ,则应编写如下内容

typedef bool (Hand::*hfunction_non_static)();
hfunction_non_static f_non_static [] =
{
    &Hand::has_sflush,
    &Hand::has_poker,
    .......
}; 
Hand h;
(h.*f_non_static[0])();

If you have static functions you should write something like 如果您有静态功能,则应编写类似

typedef bool (*hfunction_static)();
hfunction_static f_static [] = {&Hand::has_sflush, ....};
f_static[0]();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM