Function Pointer to Class Function pointer

I am writing a program where I am using one class for functionalatiy and one class for storage ("Game" and "Player" respectively). Depending on what parameters are passed to Game, different numbers of players will be created, each using a different strategy, the strategies being functions defined in Game. As such, I have a function pointer in Player that I wish to point to a strategy function in Game (declared as "void (*strat)(int);").

I am trying to use a function in Game to facilitate this transaction:

1
2
3
4
void Game::setupFunc (Player &p, void (Game::*sp)(int))
{
	p.strat = sp;
}


I am getting this error:
error C2440: '=' : cannot convert from 'void (__thiscall Game::* )(int)' to 'void (__cdecl *)(int)'

My question is how can I resolve this error? Am I going about associating a function from Game with my Player class? Thanks!
(declared as "void (*strat)(int);")


This is your problem.

You cannot convert a pointer to a global function (strat) to a non-global member function (sp) or vice versa. Nonstatic member functions take an additional hidden parameter (this) and therefore are incompatible with global functions.

You'll need to change the way strat is defined.

You also might want to consider using typedefs to make your life easier:

1
2
3
4
5
6
7
8
9
10
11
typedef void (Game::*StratFunc)(int);

// ---

StratFunc strat;

// -- 
void Game::setupFunc(Player& p, StratFunc sp)
{
  //...
}
Player::strat needs to be declared as void (Game::*strat)(int); (as sp)
Bazzy,
I need strat to be reinitialized for every instance of Player I create. I therefore cannot move it into the Game class. Is there a way I can declare it as a member of the game class without excluding it from the Player class?
I think you're misunderstanding.

The function pointer does not need to be a member of Game. It needs to point to a member of Game.

ie:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Game
{
  void AStrategyFunction(int foo)
  {
    // do stuff
  }
};

typedef void (Game::*StratFunc)(int);  // typedef it for ease

class Player
{
  // make the pointer a member of Player
  StratFunc  strat;

  Player()
  {
    // the ctor
    strat = &Game::AStrategyFunction;  // although strat is a member of
                          //  Player, it points to a member of Game
  }
};
Ah Ha! I get it now. One more issue: Game and Player are not in the same file. Thanks for your help so far!
forward declare when necessary.

See this article:

http://cplusplus.com/forum/articles/10627/
Just use boost::function and boost::bind and make life easier.

1
2
3
4
5
6
7
8
#include <boost/bind.hpp>
#Include <boost/function.hpp>

typedef boost::function<void(int)> StratFunc;

Player( Game& gm ) :
    strat( boost::bind( &Game::AStrategyFunction, gm, _1 ) )
{}


One thing though, Games don't have strategies, players do. It seems wrong to have the strategy function in Game and not Player.
Topic archived. No new replies allowed.