Function Pointer in classes

I am busy making a chess game and I am very new to void* or function pointers.

I have created the following .h file and implementation file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Command.h

#ifndef COMMAND_H
#define COMMAND_H

#include "Game.h"

class MoveCommandMemento;
class CommandMemento;
//class Game;

class Command
{
    public:
        virtual void execute() = 0;
        virtual Command* createMemento();
        virtual void setMemento(CommandMemento*) = 0;
    protected:
            Game* _reciever;
};

class MoveCommand : public Command
{
    public:
        typedef void (Game::*MoveFunc)(Coordinate&);

        MoveCommand(Game* g, MoveFunc f);

        virtual void setMemento(CommandMemento* mem);
        virtual void execute();

        private:
            MoveFunc _moveFunc;
            Game* _reciever;
            friend class MoveCommandMemento;

};

class MoveCommandMemento
{
    public:
       typedef void(Game::*MoveFunc)(Coordinate&);
        MoveCommandMemento(MoveCommand* mCMD);

    private:
        friend class moveCommand;
        Game* _reciever;
        MoveFunc _moveFunc;
};


#endif




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Command.C
#include "Command.h"

MoveCommand::MoveCommand(Game* g, MoveFunc f) : _reciever(g), _moveFunc(f) {}

void MoveCommand::setMemento(CommandMemento* mem)
{
    _reciever = mem->_reciever;
    _moveFunc = mem->_moveFunc;
}
void MoveCommand::execute()
{
    (_reciever->*_moveFunc)(Coordinate&);
}

MoveCommandMemento::MoveCommandMemento(MoveCommand* mCMD)
{
    _reciever = mCMD->_reciever;
    _moveFunc = mCMD->_moveFunc;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
My errors

[code]In constructor `MoveCommand::MoveCommand(Game*, void (Game::*)(Coordinate&))':|
will be initialized after
Command.h|31|warning:   `void (Game::*MoveCommand::_moveFunc)(Coordinate&)'|
Command.C|3|warning:   when initialized here|
In member function `virtual void MoveCommand::setMemento(CommandMemento*)':|
Command.C|7|error: invalid use of undefined type `struct CommandMemento'|
Command.h|7|error: forward declaration of `struct CommandMemento'|
Command.C|8|error: invalid use of undefined type `struct CommandMemento'|
Command.h|7|error: forward declaration of `struct CommandMemento'|
Command.C||In member function `virtual void MoveCommand::execute()':|
Command.C|12|error: expected primary-expression before '&' token| <my main prob>
Command.C|12|error: expected primary-expression before ')' token| <my main prob>
||=== Build finished: 6 errors, 3 warnings ===|

[/code]





I am unsure how to go about calling the function (Game::*moveFunc)(Coordinate&)
the command class is being build according to the COMMAND design pattern and the MEMENTO design pattern.

http://en.wikipedia.org/wiki/Command_pattern
http://en.wikipedia.org/wiki/Memento_pattern

I want MoveCommand::execute to perform the function, by the errors, it seems im doing it wrong, how should i be doing this? and Please give me tips or tell me things that are pointless in my code, as this is the first time im using function pointers.

Another problem i have is creating a memento, i have a circular dependancy, where the memento needs to copy all the values of my command, while command needse to reinstate with the memento, so they both need to access each other's data members. I have declared both Memento and MoveCommand as friends of each other. Is this a bad idea? is there a better way to do this?









Last edited on
Even after reading your description a few times, I still do not understand what you are attempting to do here. The Command pattern and the Memento pattern are orthogonal. Having a MoveCommandMemento class doesn't make much sense to me.

Typically you would have your object (I'm assuming "Game" in this case) accept commands and retain mementos for history/undo. These are two separate bits of functionality. What would typically happen is that before executing the Command, the Game would save its state to a Memento, and only then call command.execute().

I would not have the commands themselves responsible for saving the Game state. Have the Game responsible for saving its own state.

But I could be way off base here because I don't clearly understand your goal.
Last edited on
ok i see, sorry, guess i confused myself, ok dont worry too much about the memento-command pattern, i just wana know about this:

void MoveCommand::execute()
{
(_reciever->*_moveFunc)(Coordinate&);
}

how do i call parameterized function pointers? in this case.
You need to pass a Coordinate instance there. Instead, you have just copied the parameter type from the function definition.
Topic archived. No new replies allowed.