我想制作一个棋盘游戏,因为每一步都必须是有效的,所以我正在制作一个电路板副本并进行移动,以便我可以验证该移动是否有效。制作电路板副本
首先我初始化所有位置上的板为0(迭代通过板,并设置每P 0
pair<int, int> p(y, x);
board_[p] = 0;
这是抄板方法
void Board::copy(Board & gb) {
for (int y = MIN_Y; y <= MAX_Y; ++y) {
for (int x = MIN_X; x <= MAX_X; ++x) {
pair<int, int> p(y, x);
if (gb.board_.at(p) != 0) {
board_[p] = new Pieces(*gb.board_.at(p)); // **where I am confused**
} else {
board_[p] = 0;
}
}
}
}
我集装箱板是:
map<pair<int,int>, Pieces*> board_;
现在在一个平面Ÿ方法,我做董事会的副本
unsigned int play(Board & b){
b.copy(*this);
}
我的问题:既
board_[p] = new Pieces(*gb.board_.at(p)); //Pieces is a class I defined
和
board_[p] = gb.board_.at(p);
编译没有任何错误或警告。我应该使用哪一个?
其实我有:Board :: Board(board& } – HoKy22 2013-02-19 22:52:58
它本质上是一个拷贝构造函数吗? – HoKy22 2013-02-19 22:53:20
@ HoKy22它应该把'const Board&'作为一个拷贝构造函数。 – 2013-02-19 23:05:24