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

时间:2020-12-25 21:17:40


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.
Both the array and the functions are members of Hand and the array is static(please correct me if it should not).
This is what I reached:

我试图在一个.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};                                              


I get this error: hand.cpp:96:42: error: declaration of ‘hfunctions’ as array of functions.
I guess the type definition is worng so I need to know how can I make the definition right

我得到这个错误:手。错误:将“hfunction”声明为函数数组。我想类型定义是有问题的,所以我需要知道如何正确定义

3 个解决方案

#1


2  

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

将hfunction声明为指向类Hand函数返回int的指针

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

声明hfunction作为指针的数组,指向返回int的类手函数。

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

警告:C中不支持——“指向类成员的指针”


Now replace int by bool (sadly, cdecl.org doesn't understand bool); so you get the syntax of the declaration.

现在用bool替换int(遗憾的是,cdecl.org不理解bool);这样就得到了声明的语法。

For the definition, replace hFunctions by Hand::hFunctions, and add the initialization part, like you did.

对于定义,可以手工替换hfunction:hFunctions,并像您所做的那样添加初始化部分。

#2


2  

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

#3


1  

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]();

#1


2  

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

将hfunction声明为指向类Hand函数返回int的指针

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

声明hfunction作为指针的数组,指向返回int的类手函数。

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

警告:C中不支持——“指向类成员的指针”


Now replace int by bool (sadly, cdecl.org doesn't understand bool); so you get the syntax of the declaration.

现在用bool替换int(遗憾的是,cdecl.org不理解bool);这样就得到了声明的语法。

For the definition, replace hFunctions by Hand::hFunctions, and add the initialization part, like you did.

对于定义,可以手工替换hfunction:hFunctions,并像您所做的那样添加初始化部分。

#2


2  

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

#3


1  

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]();