Struct inside a struct

M

monkeydragon

hi,
i am making a struct within a struct like this..

struct upper
{
struct lower{
BYTE* row;
UINT width_row;
}
}

upper* table= new upper[100];
upper[x].lower* plates = new upper[x].lower[100];

how can i access each element in this way..
table[0].plates [0].width_row = 10; // for example
table[0].plates [1].width_row = 12;
table[n].plates [m].width_row = something;
.... so on

so that later i can do:

table[n].plates[m].row = new BYTE[table[n].plates [m].width_row];

Thx
 
R

Rolf Magnus

monkeydragon said:
hi,
i am making a struct within a struct like this..

struct upper
{
struct lower{
BYTE* row;
UINT width_row;
}
;

;


upper* table= new upper[100];
upper[x].lower* plates = new upper[x].lower[100];

upper::lower* plates = new upper::lower[100];
how can i access each element in this way..
table[0].plates [0].width_row = 10; // for example
table[0].plates [1].width_row = 12;
table[n].plates [m].width_row = something;
... so on

In this case, your upper class needs to contain an ary of lower, or - if you
need a dynamic size, a pointer to lower, like:

struct upper
{
struct lower
{
BYTE* row;
UINT width_row;
};

lower* plates;
};

Then you can do something like:

int main()
{
upper* table = new upper[100];
table[0].plates = new upper::lower[100];

table[0].plates[0].width_row = 10;

delete[] table[0].plates;
delete[] table;
}


However, I suggest using std::vector, which is easier to use than a dynamic
array, but gives the same functionality:

#include <vector>

struct upper
{
struct lower
{
BYTE* row;
UINT width_row;
};

std::vector<lower> plates;
};

int main()
{
std::vector<upper> table;
table.resize(100);
table[0].plates.resize(100);
table[0].plates[0].width_row = 10;
}

Note that there is no need for deleting the memory. That is done
automatically.
so that later i can do:

table[n].plates[m].row = new BYTE[table[n].plates [m].width_row];

Again, use a vector for that instead of raw arrays. It will also keep track
of its size.
 
M

monkeydragon

"upper" get its value change every loop..
i am wondering if when you we use table.resize
say from table.resize(10) then table.resize(20);
what will happen to data from 0-9? will it be still there?

i.e.
count = 1
do{
table.plates.resize(count)
// some routine...
++count;
}while(SOME_CONDITION);

krby_xtrm
 
R

Rolf Magnus

monkeydragon said:
"upper" get its value change every loop..
i am wondering if when you we use table.resize
say from table.resize(10) then table.resize(20);
what will happen to data from 0-9? will it be still there?

Yes. However, any pointers or iterators to the data may be invalidated.
i.e.
count = 1
do{
table.plates.resize(count)
// some routine...
++count;
}while(SOME_CONDITION);

If you want to add a value at every iteration, I'd recommend to use
push_back() instead, like:

do{
// get new value
table.plates.push_back(new_value);
}while(SOME_CONDITION);

push_back will automatically resize the vector.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top