Polymorphism and Command Pattern Problem

I am trying to implement a Command pattern with polymorphism. This is the first time I have tried either. It isnt working. The pointer to the Command class is either not accessing the Command member function "execute" or the Command class is not accessing the Actor class member function "onAction".

Here is the source:

Firstly I make an Actor that has a name and can act:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Actor
{
    public:

        std::string name; // the actors name.

        std::string message;

        void onAction()  // a actors action behaviour
        {

            message = ": I am acting! ";

        }
};


Then I create what I believe is called the abstract base class for Commands:
1
2
3
4
5
class Command
{
    public:
        virtual void execute( Actor &actor );
};


The Command points to the actor when its execute is called and will acess member function of said Actor. I then create an "Action Command":
1
2
3
4
5
6
7
8
class Action: public Command
{
    public:
        virtual void execute( Actor &actor )
        {
            actor.onAction();
        }
};


When the command is called it accesses its the member function behavior of the actor.

The biggest class in my program is an Input handling one:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Input
{
    public:

        Action actionCommand;   // an instance of an actual command

        Command * onInput = nullptr;    // an example command of user input. The input commands are all pointers to actual commands.

        Command * handleInput( bool input );    // a function that gets user input commands and returns them

        void bindInput(); // a function that sets the users input to in game commands

};


So, I declare actually instances of the different types of Commands, for now just Action. Then, I create pointers using the abstract base class Command that would be called for user inputs - for now just the one - onInput.

1
2
3
4
void Input::bindInput()
{
    onInput = &actionCommand; // point the user input to the action command
}


This function ideally would read a text file and set the keypresses / events to actual game commands but for now just hard sets it.

1
2
3
4
5
6
7
8
9
10
11
Command * Input::handleInput( bool input )
{
    if ( input == true ) // if the user inputs
    {
        return onInput;  // return the input command
    }
    else
    {
        return nullptr; // return nothing
    }
}


This function takes input ( in this case just a derpy placeholder bool ) and if the input is true the it return the onInput Command pointer, which points to the Action Command.

Now for the main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
main()
{

    Actor actor; // declare an instance of the actor
    actor.name = "Bob"; // the actors name string is delcared.

    Input inputHandler; // declare an instance of the input class

    bool input = true; // the user inputs to act
    Command * command = inputHandler.handleInput( input ); // the input handler polls for a command and returns it to the generic command pointer.
    if( command != nullptr )   // if there is a command
    {
        command->execute( actor );   // the command is given to the actor.
    }

    cout << actor.name << actor.message << endl;

    return 0;
}


I typed in plain English what my goals were and also my understanding, because as a new programmer I may not correctly understand what is happening and this might be why I wrote the code wrong. If you see problems with my understand please correct me as well as my syntax.

Thanks for your time!
Topic archived. No new replies allowed.