简体   繁体   English

从另一个类访问模板的变量

[英]Accessing variables of a template from another class

I have an issue with a small game program I'm trying to write. 我正在尝试编写一个小游戏程序的问题。 I created a template class "Board" that holds a 2D array of type "T" so that I can use the board for different types of games. 我创建了一个模板类“Board”,它包含一个类型为“T”的2D数组,这样我就可以将该板用于不同类型的游戏。 The issue is that the array (T board[SIZE][SIZE]) needs to be modified during the game. 问题是在游戏过程中需要修改数组(T板[SIZE] [SIZE])。 Another class "Othello" has a "Board" of type "Tile" which is a struct that contains two variables, "Player" (defined by another class) to state which player is in control of the tile, and two bool variables "black" and "white" to state if either player can move there. 另一个类“Othello”有一个“Tile”类型的“Board”,它是一个包含两个变量的结构,“Player”(由另一个类定义)来说明哪个玩家控制了tile,以及两个bool变量“black如果任何一名球员都能在那里移动,那就说“和”白色“。 So this is basically what it looks like: 所以这基本上是它的样子:

Board: 板:

int SIZE = 8;
template<class T>
class Board {
public:
    // class functions
private:
    T board[SIZE][SIZE]
};

Othello: 奥赛罗:

class Othello {
public:
    // class functions
private:
    // function helpers
struct Tile {
    Player current; // current tile holder (BLACK, WHITE, NEUTRAL)
    bool black; // can black capture?
    bool white; // can white capture?
    unsigned location; // number of the tile, counted from left to right
};

Board<Tile> othelloBoard; // board for the game

int bCounter; // counter for black units
int wCounter; // counter for white units

User playerOne; // information for first player
User playerTwo; // information for second player
};

The issue is that I can't modify the "Board" directly through the "Othello" class (I can't access the board through the Othello class, so othelloBoard.board[x][y].current = WHITE; for instance doesn't work), but I can't define a modifier function within "Board" since the type can be anything. 问题是我无法通过“奥赛罗”类直接修改“Board”(我无法通过Othello类访问该板,所以othelloBoard.board [x] [y] .current = WHITE;例如不起作用),但我不能在“Board”中定义修饰符函数,因为类型可以是任何东西。 I can't seem to wrap my head around how I would go about doing this. 我似乎无法理解我将如何做到这一点。 Maybe I'm missing something really simple. 也许我错过了一些非常简单的事情。 This isn't a school project, I'm revisiting an old project from my first C++ course and trying to rebuild it myself. 这不是一个学校项目,我正在重新审视我的第一个C ++课程中的一个旧项目,并尝试自己重建它。 Thanks for any help! 谢谢你的帮助!

The question is: what is a Board? 问题是:什么是董事会? And what abstraction does it provide (if any)? 它提供了什么抽象(如果有的话)? You didn't show the class function here so I don't really now. 你没有在这里显示类功能,所以我现在不是。 As you seem to try to use it, it seems pretty useless. 当你似乎试图使用它时,它似乎没用。 Anyway with a very shallow encapsulation, you can just provide accessors for Tiles: 无论如何,封装非常浅,您只需提供Tiles的访问器:

template<class T, int SIZE = 8>
class Board {
public:
    T &tileAt(int x, int y) {
        assert(x>=0 && x < SIZE && y>=0 && y<SIZE);
        return board(x, y);
    }

    // class functions

private:
    T board[SIZE][SIZE]
};

(note that I moved the SIZE as a template parameters, so that your future Tic-Tac-Toe game can instantiate another version of the template changing the size) (请注意,我将SIZE作为模板参数移动,以便您未来的Tic-Tac-Toe游戏可以实例化更改大小的模板的另一个版本)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM