Exposing non-modifiable version of the nested member variables

时间:2018-06-19 11:13:33

标签: c++ game-development

I have a class for a small card game, with a nested pointer structure. There are several game objects, that needs to be rendered on the display. However, the class that will render this objects, should not be able to modify any of them. I don't want to make the renderer class a friend class. Instead, I want to return a non-modifiable version of the game objects from a function.

How can I expose the non-modifiable version of the "columns" array with this given nested pointer structure?

Here is the header for the class:

class Card;

// Game objects
using Deck = std::array<Card*, 52>;
using Column = std::vector<Card*>;

class Game {

    // Deck
    Deck deck;

    // Columns
    Column* columns[7];
    Column* collectorColumns[4];
    Column* availableCardsColumn;

    // Current state of the game
    bool playing = false;

    // Creates a new deck
    void createDeck();

    // Creates columns
    void createColumns();

    // Deals the cards for the initial state of the game
    void dealCards();

    // Clears all objects
    void clear();

public:

    // Starts a new game
    void start();

    // True if the game is finished, false otherwise
    bool isFinished();

    // Move card from one column to another
    void moveCard(int from, int to);

    // Game renderer is a friend
    friend void GameRenderer::renderGame(const Game& game) const;

};

0 个答案:

没有答案