简体   繁体   English

指向类中多维数组的指针

[英]pointers to a multidimensional array in classes

I'm trying to get to the bottom of an error, I wonder if you could help please? 我正在尝试找出错误的根源,我想知道您是否可以提供帮助? I'm having a bit of an issue with array pointers at the moment. 目前,我在数组指针方面存在一些问题。 I have three classes, one parent, and two children so to speak. 可以这么说,我有三个班级,一个父母,两个孩子。 One of the children has a 2D array of type struct, and I'm trying to access the elements of this from the other child. 一个孩子有一个二维数组struct类型,我正试图从另一个孩子访问此元素。

I was wondering, is this code valid with the correct format/syntax for my array pointers? 我想知道,此代码对数组指针的正确格式/语法是否有效? OChild1 creates and fills out the 2D array, I'm saving a pointer to that in Parent, and passing that pointer to OChild2, and then plan on using the contents of the array for further processing. OChild1创建并填充2D数组,我将指向该数组的指针保存在Parent中,并将该指针传递给OChild2,然后计划使用该数组的内容进行进一步处理。

struct BOARDTILE
{
    float fPosX;
    float fPosY;

    BOARDTILES()
    {
        fPosX = 0.0f;
        fPosY = 0.0f;
    }
};

class CChild1
{
    public:
        BOARDTILE BoardTileArray[18][18];

    CChild1()
    {
    }

    writeBoardTileArray()
    {
        for (int i = 0; i <= 17; i++)
        {
            for (int j = 0; j <= 17; j++)
            {
                BoardTileArray[i][j].fPosX = (float) i * 5.0f;
                BoardTileArray[i][j].fPosY = (float) j * 7.0f;
            }
        }
    }
};

class CChild2
{
    public:
        BOARDTILE (*pBoardTileArray)[18][18];
        float fPosX;
        float fPosY;

    CChild2()
    {
    }

    void readBoardTileArray()
    {
        for (int i = 0; i <= 17; i++)
        {
            for (int j = 0; j <= 17; j++)
            {
                fPosX = (*pBoardTileArray[i][j]).fPosX;
                fPosY = (*pBoardTileArray[i][j]).fPosY;
                cout << fPosX;
                cout << fPosY;
            }
        }
    }

};

class CParent
{
    public:
        BOARDTILE (*pBoardTileArray)[18][18];
        CChild1 OChild1;
        CChild2 OChild2;

    CParent()
    {
        OChild1.writeBoardTileArray();
        pBoardTileArray = &(OChild1.BoardTileArray);
        OChild2.pBoardTileArray = pBoardTileArray;
    }
};

To store address of a two dimensional array, you would need a pointer to pointer, 要存储二维数组的地址,您需要一个指针,

Without typedef: 没有typedef:
You would declare a member pointer as: 您可以将成员指针声明为:

BOARDTILE (*pBoardTileArray)[18];

and then in the constructor, you would write: 然后在构造函数中,您将编写:

    pBoardTileArray = OChild1.BoardTileArray;

But as you can see it makes the code complex. 但是正如您所看到的,它使代码变得复杂。 Using typedef can simplify it: 使用typedef可以简化它:

With typedef: (Recommended way) 使用typedef :(推荐方式)
With typedef you can write the following in the global scope: 使用typedef可以在全局范围内编写以下内容:

typedef BOARDTILE BOARDTILE_ar18_t[18];

and then you can declare member pointer as: 然后可以将成员指针声明为:

BOARDTILE_ar18_t* pBoardTileArray;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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