c++ - pointers to a multidimensional array in classes -
i'm trying bottom of error, wonder if please? i'm having bit of issue array pointers @ moment. have 3 classes, 1 parent, , 2 children speak. 1 of children has 2d array of type struct, , i'm trying access elements of other child.
i wondering, code valid correct format/syntax array pointers? ochild1 creates , fills out 2d array, i'm saving pointer in parent, , passing pointer ochild2, , plan on using contents of array further processing.
struct boardtile { float fposx; float fposy; boardtiles() { fposx = 0.0f; fposy = 0.0f; } }; class cchild1 { public: boardtile boardtilearray[18][18]; cchild1() { } writeboardtilearray() { (int = 0; <= 17; i++) { (int j = 0; j <= 17; j++) { boardtilearray[i][j].fposx = (float) * 5.0f; boardtilearray[i][j].fposy = (float) j * 7.0f; } } } }; class cchild2 { public: boardtile (*pboardtilearray)[18][18]; float fposx; float fposy; cchild2() { } void readboardtilearray() { (int = 0; <= 17; i++) { (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 2 dimensional array, need pointer pointer,
without typedef:
declare member pointer as:
boardtile (*pboardtilearray)[18];
and in constructor, write:
pboardtilearray = ochild1.boardtilearray;
but can see makes code complex. using typedef
can simplify it:
with typedef: (recommended way)
typedef
can write following in global scope:
typedef boardtile boardtile_ar18_t[18];
and can declare member pointer as:
boardtile_ar18_t* pboardtilearray;
Comments
Post a Comment