GroniumArgor
GroniumArgor

Reputation: 113

C++ - Initializing an empty array on class

i'm having some trouble on initializing an empty array ,or so i call it, in a class. basically i want to fill a 2d array with 0.

here's my code:

Board.cpp

#include "Board.h"
#include <string>
#include <iostream>
#include "Player.h"

using namespace std;

Board::Board()
: _board{ { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }
{
}

void Board::drawBoard()
{
    for (int i = 0; i < 4; i++){
        for (int j = 0; j < 4; j++){
            if (i == 0 && j == 0){
                cout << " ";
            }
            else if (i == 0){
                cout << " " << j << " ";
            }
            else if (j == 0){
                cout << i;
            }
            else {
                if (_board[i][j] == 0)
                    cout << "| " << "|";
                else
                    cout << "|" << _board[i][j] << "|";
            }
        }
        cout << endl;
    }
}

void Board::playerInput(char input)
{
}

here is Board.h:

#pragma once
#include <string>
#include <iostream>

class Board
{
public:
    Board();
    void drawBoard();
    void playerInput(char input);


private:
    char _board[3][3];
    char _input;
};

is that a right way to initialize an array? i read some articles but it just confuses me more. i'm sorry if i sound stupid. but i need your help. thank you.

Upvotes: 0

Views: 5682

Answers (3)

juanchopanza
juanchopanza

Reputation: 227418

All you need to do is value initialize the data member:

Board::Board() : _board() {}

This will zero-initialize all the elements of _board, and also works for C++03.

Upvotes: 2

user3052734
user3052734

Reputation:

The method juanchopanza is using is the current set by c++11 However, I would suggest some small changes. If all the default constructor is doing is initializing the array I would instead initialize it in the header file. Like this

#pragma once
#include <string>
#include <iostream>

class Board
{
public:
    Board()=default; //becomes compiler constructed 
    void drawBoard();
    void playerInput(char input);


private:
    char _board[3][3]{};//added {} here
    char _input;
};

This way the array is initialized BEFORE the constructor is called and the constructor is made by the compiler. The advantage here is that there is less code needed by you and it is scalable for additional constructor if you desire.

for example:

if you add

Board(char input) for input all that is needed is:

Board(char input)
{
   _input=input;// no need to repeat initialization code for _board array

}

Upvotes: 1

tdemay
tdemay

Reputation: 738

Depending on your compiler, you should be able to do this (notice use of parenthesis instead of brackets):

Board::Board()
: _board( { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } )
{
}

Ref: https://stackoverflow.com/a/2409882/1932930

Looks like you are using VS2013. It looks like VS2013 supports it. Ref: http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport

Upvotes: 0

Related Questions