Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Three questions

Author: Gerd Isenberg

Date: 03:03:13 11/09/02

Go up one level in this thread


On November 09, 2002 at 04:44:14, Sune Fischer wrote:

>First Question:
>I want to support some variants of chess, mostly fischer-random.
>For this I need a member function pointer to point to a member function.
>I must have the syntax wrong cause the compiler complains, this is what I do:
>
>class BOARD {
>public:
>  void (*pGenCastleMoves)();  // is this correct declaration? no
>  void GenNormalCastleMoves();
>  void GenFischerCastleMoves();
>...
>}
>
>I try and intialise it by:
>
>void GAME::SetNormal() {
>  variant=NORMAL;
>  Chessboard.pGenCastleMoves=Chessboard.GenNormalCastleMoves;
>}
>
>Where GAME is a different class that controls settings for the entire game, like
>the variant. It doesn't work though, I get:
>
>error C2440: '=' : cannot convert from 'void (__thiscall BOARD::*)(void)' to
>'void (__cdecl *)(void)'
>        There is no context in which this conversion is possible
>
>I have tried variations, like:
>  BOARD::pGenCastleMoves=BOARD::GenNormalCastleMoves;
>doesn't work either. What am I doing wrong?
>

Hi Sune,

you declared a function pointer but no pointer to member functions.
Pointer to member function need an pointer to an object and they act like an
offset. There are explicitely two new atomic operators in C++, ".*" and "->*",
to call member fuctions via pointer.

class BOARD {
public:
  void (BOARD::*pGenCastleMoves)();
  void GenNormalCastleMoves();
  void GenFischerCastleMoves();
...
}

void GAME::SetNormal() {
  variant=NORMAL;
  Chessboard.pGenCastleMoves=Chessboard.GenNormalCastleMoves;
}

I use arrays of function pointers a lot in this way:

	typedef	void (CSearchTree::*PTR_DOMOVE)(CNode &node);
	static PTR_DOMOVE m_scDoMove[SMOVE::MK_NUMBER_OF_KINDS];
	__forceinline void DoMove(const CNode &fromnode, CNode &tonode)	{
          ....
          (this->*m_scDoMove[tonode.m_Move2ThisNode.kind])(tonode);}

For your purpose i would prefere an abstract base class with two concrete
derivates, where you must overload the pure virtual GenCastleMoves routine:

class BOARDBASE {
public:
  virtual void GenCastleMoves() = 0;
...
}

Gerd

<snip>



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.