我想製作一個棋盤遊戲,因爲每一步都必須是有效的,所以我正在製作一個電路板副本並進行移動,以便我可以驗證該移動是否有效。製作電路板副本
首先我初始化所有位置上的板爲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