简体   繁体   中英

A pointer to 2D Array of Object Type Pointers

I just wanted to ask a quick question.

I have a class called "ChessPiece"

#ifndef CHESSPIECE_H
#define CHESSPIECE_H

#include "Globals.h"

// Abstract class for inheritence
class ChessPiece {
public:
    // Constructor
    ChessPiece(bool isWhite) : m_isWhite(isWhite) {
    }
    // No dynamic allocation
    ~ChessPiece(void) {}

    // pure virtual functions
    virtual CellLocation *listAvailableMoves(void) = 0;
    virtual char getPieceType(void) = 0;
    virtual ChessPiece *clonePiece(void) = 0;

    // ACCESSORS, MUTATORS
    // isWhite member
    bool isWhite(void) const{
        return m_isWhite;
    }

    void setIsWhite(bool isWhite) {
        m_isWhite = isWhite;
    }

protected:
    bool m_isWhite;
};
#endif

and I have a variable like this:

ChessPiece *m_gameBoard[8][8];

I wanted to know how can I define a pointer to this variable? I thought it'd be something like ChessPiece *(*pGameBoard)[8][8] but it's not what I want. Say for example that I want to make call like this *pGameBoard[2][2]->isWhite() (this doesn't work) How can I do this?

Thanks in advance for your answers.

ChessPiece *m_gameBoard[8][8];

ChessPiece * (*pGameBoard)[8];

ChessPiece *(*pGameBoard)[8][8]; does declare a pointer to the type that m_gameBoard is in ChessPiece *m_gameBoard[8][8]; . You would use it with, for example:

(*pGameBoard)[2][2]->isWhite()

However, in most situations, it is satisfactory to point to an element of the outermost array instead of the entire array, as with:

ChessPiece *(*pGameBoard)[8];

A pointer to the entire array and a pointer to the first element of the array are effectively the same address. However, because the latter is equivalent to the former with the * operator already applied, you could use it like this:

pGameBoard[2][2]->isWhite()

Note that, with the first declaration, you would assign or initialize it using a pointer to the entire array, as with:

ChessPiece *(*pGameBoard)[8][8] = &m_gameBoard;

while, in the latter declaration, you would assign or initialize it using a pointer to the first element of the array, as with;

ChessPiece *(*pGameBoard)[8][8] = &m_gameBoard[0];

or, equivalently because the unadorned array is automatically converted to a pointer to its first element, with:

ChessPiece *(*pGameBoard)[8][8] = m_gameBoard;

I'm not sure how you're allocating space for those ChessPiece objects. Whether I try it allocating on the stack or dynamically, I get compilation errors from g++ because there's no default constructor. When you allocate an array of instances on the stack or using new , the default constructor is called for each instance created.

In your ChessPiece class add a default constructor:

ChessPiece() : m_isWhite(false) {}

Then you can allocate the array on the stack as:

ChessPiece pieces[8][8];

And access that array using:

pieces[i][j].isWhite();

This seems more straightforward to me, let me know what you think.

Also, you may want to consider organizing using a Board object, which represents a grid of Cell objects. Each Cell tracks whether it has a piece in it, and if it does, can return a reference/copy of that piece. To me, that's clearer conceptually, but there's nothing wrong with your way, either. :)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM