我的目标是在被修建后被毁坏

时间:2015-03-11 03:55:26

标签: c++ arrays constructor boolean destructor

我正在尝试使用我创建的名为Grid的类构建一个二维布尔数组。 Grid对象是另一个名为GameOfLife的类的私有成员类。每当我使用参数belove创建一个GameOfLife对象时,首先使用默认构造函数创建Grid对象,然后使用带参数的构造函数再次创建它,然后由于某种原因Grid的解构函数运行并删除所有内容?我真的没有想法:p我在Eclipse Luna上运行MinGW GCC。

Main.cpp的

const int HEIGHT = 25;
const int WIDTH = 25;
#include <iostream>
#include "GameOfLife.h"

int main(int argc, const char * argv[]) {
    GameOfLife game = GameOfLife(HEIGHT, WIDTH, false);
    game.play();
    return 0;
}

Grid.h

#ifndef __Game_Of_Life__Grid__
#define __Game_Of_Life__Grid__

#include <stdio.h>

class Grid {
public:
    Grid(int y, int x, bool state);
    Grid();
    void allocate(int x, int y, bool state);
    void deallocate();
    void set(int x, int y, bool state);
    bool get(int x, int y);
    void setAll(bool state);
    void switchBoards();
    ~Grid();
private:
    bool ** oldGeneration;
    bool ** newGeneration;
    int height;
    int width;
};
#endif /* defined(__Game_Of_Life__Grid__) */

Grid.cpp

#include "Grid.h"

Grid::Grid(int y, int x, bool state) {
    allocate(x, y, state);
}
void Grid::allocate(int x, int y, bool state) {
    height = y;
    width = x;
    oldGeneration = new bool*[height];
    newGeneration = new bool*[height];
    for (int i = 0; i < height; i++) {
        oldGeneration[i] = new bool[width];
        newGeneration[i] = new bool[width];
    }
}
Grid::~Grid() {
    deallocate();
}
void Grid::switchBoards() {
    bool ** temp = oldGeneration;
    oldGeneration = newGeneration;
    newGeneration = temp;
    delete temp;
}  
bool Grid::get(int x, int y) {
    return oldGeneration[y][x];
} 
void Grid::set(int x, int y, bool state) {
    newGeneration[y][x] = state;
}  
void Grid::deallocate() {
    if (oldGeneration != NULL || newGeneration != NULL) {
        for (int i = 0; i < height; i++) {
                delete [] oldGeneration[i];
                delete [] newGeneration[i];
        }
        delete [] oldGeneration;
        delete [] newGeneration;
    }
    return;
}
Grid::Grid() {
    oldGeneration = NULL;
    newGeneration = NULL;
    width = 0;
    height = 0;
}
void Grid::setAll(bool state) {

    for (int i = 0; i < height; i++) {
        for (int n = 0; n < width; n++) {
            newGeneration[i][n] = state;
        }
    }
}

GameOfLife.h

#ifndef __Game_Of_Life__GameOfLife__
#define __Game_Of_Life__GameOfLife__

#include <stdio.h>
#include "Grid.h"
#include <iostream>


class GameOfLife {
private:
    Grid board;

public:

    GameOfLife(int y, int x, bool state);
    GameOfLife();
    ~GameOfLife();
    void play();
    void welcome();
    void makeBoard();
    void updateBoard();
    int findAliveNeighbours(int x, int y);
};

#endif /* defined(__Conway__GameOfLife__) */

GameOfLife.cpp

#include "GameOfLife.h"
const int WIDTH = 100;
const int HEIGHT= 75;



GameOfLife::GameOfLife(int y, int x, bool state) {
    board = Grid(y, x, state);

}
GameOfLife::GameOfLife() {
    board = Grid();
}

GameOfLife::~GameOfLife() {
    board.deallocate();
}
void GameOfLife::play() {

    welcome();
    makeBoard();

    for (int i = 0; i < HEIGHT; i++) {
        for (int n = 0; n < WIDTH; n++) {
            std::cout << board.get(n,i) << " ";
        }
        std::cout << std::endl;
    }
    updateBoard();
    std::cout << std::endl;
    for (int i = 0; i < HEIGHT; i++) {
        for (int n = 0; n < WIDTH; n++) {
            std::cout << board.get(n,i) << " ";
        }
        std::cout << std::endl;
    }

}
void GameOfLife::makeBoard() {
    int x1,x2,x3,x4, y1,y2,y3,y4;
    x1 = 10; y1 = 10;
    x2 = 10; y2 = 11;
    x3 = 10; y3 = 12;
    x4 = 11; y4 = 13;
    int x5 = 0; int y5 = 0;
    board.set(x1, y1, true);
    board.set(x2, y2, true);
    board.set(x3, y3, true);
    board.set(x4, y4, true);
    board.set(x5, y5, true);
}
void GameOfLife::welcome() {

        std::cout << "Welcome to Conway's Game Of Life"
                  << std::endl;
}

1 个答案:

答案 0 :(得分:2)

GameOfLife::GameOfLife(int y, int x, bool state) {

    // board is a member variable that gets initialized
    // with the default constructor.
    // Then it gets replaced by assignment with a different
    // Grid object. The temporary object gets deleted at
    // the end of the line.
    board = Grid(y, x, state);
}

将实施更改为:

GameOfLife::GameOfLife(int y, int x, bool state) : board(y, x, state) {}

同样,将默认构造函数更改为:

GameOfLife::GameOfLife() {}

需要修复的更重要的问题是您要打破The Rule of Three

您需要在Grid中添加复制构造函数和复制赋值opertor的正确实现。

另一个更好的选择是将Grid的内部数据更改为

std::vector<std::vector<bool>> oldGeneration;
std::vector<std::vector<bool>> newGeneration;

然后,编译器生成的复制构造函数和复制赋值运算符就足够了。