2


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:

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

2
  • What's the question ? Commented Jul 23, 2013 at 21:41
  • 2
    What is it you're actually trying to achieve here? Commented Jul 23, 2013 at 21:41

3 Answers 3

3

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:

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'


Now replace int by bool (sadly, cdecl.org doesn't understand 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.

Sign up to request clarification or add additional context in comments.

1 Comment

cdecl.org, I didn't know that tool, thanks!! If it worked for c++ would be even better
2

Both the array and the functions are members of Hand and the array is static(please correct me if it should not).

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

4 Comments

"you should not" - i guess this means "you should not implement this the way you did"; the question was "should the array be static?". Anyway +1.
Yes, it was about not implementing it this way. Usually when you need to create an array of functions to abstract behavior, that means "go virtual".
I will have to research in order to understand this answer. Thanks for the effort.
My point was that your has_sflush, has_poker (and so on) functions should not be a set of member functions in Hand, but a hierarchy of classes outside of it.
1

If you have non static member functions with no arguments and returning bool you should write something like

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 Comment

Thanks! This is not exactly what I was asking because I had trouble when I had to tell the compiler that "hfunctions is member of Hand" in the .cpp file. Anyway I have learned a lot from your answer, for instance that the usage of typedef makes this easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.