I have a 2D char array filled with '_' 's, I have made a static array and I wanted to change it to dynamic so you can change the size of the board, do I need to assign a pointer in public and int's in private? Or will I have to make a new array completely? Thanks :)
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class board
{
public:
void makeBoard()
{
for (int i = 0; i < 11; yaxis++)
{
for (int j = 0; j < 11; xaxis++)
{
opening[i][j] = '_';
}
}
}
private:
char placement[11][11];
};
As suggested, std::vector
is probably the way to go. Here is a small example that might help you:
#include <vector>
class board
{
public:
board(int x, int y)
{
resize(x, y);
}
void resize(int x, int y)
{
// make sure input is OK
mBoard.clear();
for (int i = 0; i < x; ++i)
mBoard.emplace_back(std::vector<char>(y, '_'));
}
private:
std::vector<std::vector<char>> mBoard; // 2 dimensional std::vector
};
And create by
board my_board(11, 11);
my_board.resize(2, 10); // Resize afterwards
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.