Author: Gerd Isenberg
Date: 10:50:38 10/29/03
Go up one level in this thread
On October 29, 2003 at 12:41:36, Michel Langeveld wrote:
>>Hi Michel,
>>
>>I guess forEachGame is a static class function.
>>Then you have to use explicite class specifiers in your typedef:
>>
>>typedef void (PGNtoScreen::*on_game_function_type)(char *moves);
>>
>>Gerd
>
>Hi Gerd,
>
>Thanks for looking.
>
>It's actually a static class function. I think the problem is that the compiler
>doesn't know what object to use for calling this function. I made a complete
>code that shows the same problem. Still puzzled.
>
>#include <iostream>
>#include <stdlib.h>
>
>using namespace std;
>
>#define MAXBUF_SIZE 1000
>
>//typedef void (*line_event_type)(char *line);
>typedef void (PGNtoScreen::*line_event_type)(char *line);
>
>class PGNParse
>{
>public:
> PGNParse()
> {
> }
>
static
> void parse(char *strTextFile, line_event_type lineEvent)
> {
> char line[MAXBUF_SIZE];
>
> FILE *f = fopen(strTextFile, "r");
> while (!feof(f))
> {
> if (fgets(line, MAXBUF_SIZE, f) == NULL) continue;
>
> lineEvent(line);
> }
> }
>};
What's the reason to use a PGNParse class here at all, so far no data members -
it's more like name spacing.
>
>class PGNtoScreen
>{
>public:
> PGNtoScreen()
> {
> lines = 0;
> }
>
> void PGNtoScreen::forEachLine(char *strLine)
> {
> cout << strLine;
> lines++;
> }
>
> void PGNtoScreen::doit(char *strFileName)
> {
> PGNParse p; // sizeof(p) == 0 !
>
> p.parse(strFileName, forEachLine); //gives error C2664
>
> cout << "Number of lines printed: " << lines << endl;
> }
>
>private:
> int lines;
>};
>
Ok, that's a "real" class with sizeof(int) and forEachLine modifies the member
"lines". Therefore calling a none static member function via pointer is not
possible in C-function pointer manner.
For your purpose, in C++ there are better ways rather than passing function
pointers as parameter around. Late binding at runtime, call a (pure) virtual
"lineEvent"-function in your base class containing functions like PGNParse (or
parseGameArchive?), implementing all common stuff. And do concrete
implementations of this functions in deriverd classes to handle pgn and other
formats.
In C++ there are pointer to member functions called via an object of this class.
They act more like an offset or function pointer index.
In C++ there are special _atomic_ operators ".*" and "->*" for that.
E.g. i use it in make/unmake move to implement a kind of jump-table:
class CSearchTree public ...
{
typedef void (CSearchTree::*PTR_DOMOVE)(CNode &node);
static PTR_DOMOVE m_scDoMove[SMOVE::MK_NUMBER_OF_KINDS];
__forceinline void CSearchTree::DoMove(const CNode &fromnode, CNode &tonode)
{
...
(this->*m_scDoMove[tonode.m_Move2ThisNode.kind])(tonode);
// this is required here!
...
}
...
void DoMove_MK_SINGLE_PAWN(CNode &node);
void DoMove_MK_DOUBLE_PAWN(CNode &node);
...
};
// in some cpp file
CSearchTree::PTR_DOMOVE CSearchTree::m_scDoMove[SMOVE::MK_NUMBER_OF_KINDS] =
{
....
DoMove_MK_SINGLE_PAWN,
DoMove_MK_DOUBLE_PAWN,
....
};
Cheers,
Gerd
>void main()
>{
> PGNtoScreen o;
> o.doit("docc2003.pgn");
>}
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.