0

So I was making a programm with a class bingocard and a class player. Each player can have a maxium number od bingocards which you give it in the constructor of the player class. In the constructor we also have a dynamic array where we save each bingocard which is a 4x4 matrix (2d array).

class Bingocard
{
private:
    unsigned int card[4][4];

public:
    Bingocard();
    Bingocard(unsigned int Card[4][4]);
    Bingocard(const Bingocard& Card);
    Bingocard operator=(const Bingocard& Card);
};

BingocardBingocard()
{
}

Bingocard::Bingocard(unsigned int Card[4][4])
{
for (int i=0; i<4; i++)
    for (int j=0; j<4; j++)
        card[i][j] = Card[i][j];
}

Bingocard::Bingocard(const Bingocard& Card)
{
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            card[i][j] = Card.card[i][j];
}

Bingocard Bingocard::operator=(const Bingocard& Card)
{
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            this->card[i][j] = Card.card[i][j];

    return (*this);
}

class Player
{
private:
    unsigned int maxNumberCards;
    Bingocard* cards;
    unsigned int numberCards;

public:
    Player(unsigned int k);
    void newCard(Bingocard* Card);
    ~Player();
};

Player::Player(unsigned int k)
{
    numberCards = 0;
    maxNumberCards = k;
    cards = new Bingocard[maxNumberCards];
}

void Player::newCard(Bingocard* card)
{
    if (numberCards < maxNumberCards) {
        cards[anzahlKarten] = *card; //Assign operator is triggered;
        anzahlKarten += 1;
    }
    else
        cout << "Maximum cards reached! Adding more cards is not possible now." << endl;
}

Player::~Player()
{
delete[] cards;
}

I have now that dynamic array called cards in the class Player and I want to access each of its elements but I dont know which command to use. I know I can make a function for example getElement and give it the row and column I want as parameters, but I was wondering if I can do that the same thing without a function. I tried (cards[a])[b][c] but it gives an error back. I need this to work so I can check each number on the card for a bingo.

CryoDrakon
  • 268
  • 1
  • 3
  • 12
  • 1
    You can [overload the indexing `operator[]()`](http://stackoverflow.com/questions/4421706/operator-overloading). – πάντα ῥεῖ Jan 13 '16 at 13:11
  • `cards[a].card[b][c]` would work, except for the fact that `card` is private. An accessor function is not a bad idea in that case. – Bo Persson Jan 13 '16 at 13:31
  • @πάνταῥεῖ if I want to overload that operator, should I do it in the Bingocard class or Player class. And does the [] refer to the dynamic array in this case? – CryoDrakon Jan 13 '16 at 13:53
  • @HeatTheIce I think in the `Player` class that holds `cards` actually. Or provide an extra wrapper for the `cards` array. – πάντα ῥεῖ Jan 13 '16 at 13:55
  • @πάνταῥεῖ so as return type I should have unsigned int [4][4] ? – CryoDrakon Jan 13 '16 at 14:48
  • @HeatTheIce Probably better something like `int [4][4] &`. But I'd recommend using `std::array,4>` as member in `BingoCard` rather than the raw array. It will make your life easier. – πάντα ῥεῖ Jan 13 '16 at 14:52

0 Answers0