My program is seg faulting when the copy constructor is invoked. This is what my constructor looks like for my Grid class:
Grid::Grid(unsigned int grid_size) {
size = grid_size;
grid = new char *[size];
for(int i = 0; i < size; i++) {
grid[i] = new char[size];
}
}
And, this is my copy constructor that is causing the problem:
Grid::Grid(Grid const &other_grid) {
size = other_grid.size;
grid = new char *[other_grid.size];
for(int i = 0; i < size; i++) {
grid[i] = new char[size];
}
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
grid[i][j] = other_grid.grid[i][j];
}
}
}
Destructor
Grid::~Grid() {
for(int i = 0; i < size; i++) {
delete [] grid[i];
}
delete [] grid;
}
operator = overloading
Grid & Grid::operator=(Grid const &other_grid) {
size = other_grid.size;
grid = new char *[other_grid.size];
for(int i = 0; i < other_grid.size; i++) {
for(int j = 0; j < other_grid.size; j++) {
grid[i][j] = other_grid.grid[i][j];
}
}
return *this;
}