1

Following is a simplified header file detailing three Classes. I want to be able to keep the pointer in my "Game" class private, and allow Introduction to modify it. However, as is, this is not working. As Introduction is a derivative of GameState, I thought I would be able to modify this pointer? Examples had shown that this was possible. I don't really want to move this to the Public space within Game.

class Introduction;
class Game;
class GameState;

class GameState
{
    public:

    static Introduction intro;

    virtual ~GameState();
    virtual void handleinput(Game& game, int arbitary);
    virtual void update(Game& game);

};


class Introduction : public GameState
{
public:

    Introduction();

    virtual void handleinput(Game& game, int arbitary); 

    virtual void update(Game& game);

};


class Game
{
public:

    Game();

    ~Game();

    virtual void handleinput(int arbitary);

    virtual void update();

private:

    GameState* state_;

};

The example I was following was here...http://gameprogrammingpatterns.com/state.html

EDIT: I am wanting to do something like this...

void Introduction::handleinput(Game& game, int arbitary) 
        {
            if (arbitary == 1)
            std::cout << "switching to playing state" << std::endl;
            game.state_ = &GameState::play;
        }

EDIT: Thank you for the responses, I think getters and setters are the way to go. And I apologise that the problem was not clear. The problem was that I did not understand the implementation I was trying to follow. I still don't understand it, but clearly there are ways to accomplish the same thing.

3 Answers3

1

How about a getter and an setter?

class Game
{
public:

   ....
   GameState * getGameState() const { return state_; }

   void setGameState(GameState * newState) { state_ = newState; }

   ....

private:

    GameState* state_;
}
robor
  • 2,969
  • 2
  • 31
  • 48
1

I see two possible solutions.

Using Friend class

You can declare friend classes in your Game class.

Something like:

class Game {
 public:
  // ...
 private:
  // ...
  friend class Introduction;
};

In this way, the class Introduction will be able to access to private member of Game class and modify it.


Getters And Setters

If you want to preserve data hiding principle, you can just provide a public member in order to modify the state of your game.

Here, an example:

class Game {
 public:
   void setNewState(GameState* setter) noexcept;
   const GameState* getCurrentState() const noexcept;
   // ...
};
BiagioF
  • 9,368
  • 2
  • 26
  • 50
  • Using **Getters and Setters** should be the first priority solution, as using **Friend Functions** is against the principle of Encapsulation, and whenever possible should be avoided. – Ahmad Khan Aug 29 '16 at 16:56
  • It depend on the design choice. Sometimes *OOP* is just an expensive overload. If those two class are strictly close, *friend* could be a good choice. In general, and your design is OOP, then you're absolutely right! Keep principle of Encapsulation and using getters and setters. – BiagioF Aug 29 '16 at 17:00
0

You can make the pointer Protected and make Game a friend to GameState, to allow Game to access protected members in GameState. But as the above comments indicate, its not really clear what you are actually asking.

Totte Karlsson
  • 1,261
  • 1
  • 20
  • 55