J
Jacek Dziedzic
Hi!
Say a have a class called black_box which, among other
things, contains an array of cells like this:
class black_box {
private:
// ...
public:
black_box_details_struct details;
cell cells[10000000]; // let's assume this is fixed to 10M
};
"details" is a struct that contains some information about
the metrics of the black_box, data in this structure is
relevant to the operation of members of the cells array.
A cell is a tiny class itself, that stores a state (say, an int)
and has some methods:
class cell {
private:
int internal_state;
// ...
public:
// ...
void update_state();
};
Now... the problem is that cell::update_state() needs some
information stored in black_box::details (because it contains
the information on cells partitioning, size, etc.).
So basically it's a "how to get a pointer to parent" issue.
I've googled a bit for a solution and found two ways to deal
with this, one was a hack almost, using an offsetof macro
that looked absolutely nasty and not portable. The other
solution was to store a pointer to parent in all children.
Obviously I can't afford storing a pointer in every cell
because that would mean 40MB of wasted data on my
system. I can't store a static pointer in the cell class either,
because I could have cells in various instances of the
black_box class that would obviously require different
pointers.
Is there anything I can do? I've been pondering on two
ways to go around this...
1) move the methods from class cell to class black_box
and hence have something like this
black_box::update_one_cell(cell &which);
but I feel that the cell updating method belongs more to the
cell class than to the underlying black_box class, so I have
some doubts.
2) the other way I thought of was to pass a pointer to
the relevant black_box as an extra parameter to each cell
method, like this:
cell::update(black_box *silly_parent_pointer);
So... which would be better (I don't like either), or is there any
other, more natural, workaround?
TIA,
- J.
Say a have a class called black_box which, among other
things, contains an array of cells like this:
class black_box {
private:
// ...
public:
black_box_details_struct details;
cell cells[10000000]; // let's assume this is fixed to 10M
};
"details" is a struct that contains some information about
the metrics of the black_box, data in this structure is
relevant to the operation of members of the cells array.
A cell is a tiny class itself, that stores a state (say, an int)
and has some methods:
class cell {
private:
int internal_state;
// ...
public:
// ...
void update_state();
};
Now... the problem is that cell::update_state() needs some
information stored in black_box::details (because it contains
the information on cells partitioning, size, etc.).
So basically it's a "how to get a pointer to parent" issue.
I've googled a bit for a solution and found two ways to deal
with this, one was a hack almost, using an offsetof macro
that looked absolutely nasty and not portable. The other
solution was to store a pointer to parent in all children.
Obviously I can't afford storing a pointer in every cell
because that would mean 40MB of wasted data on my
system. I can't store a static pointer in the cell class either,
because I could have cells in various instances of the
black_box class that would obviously require different
pointers.
Is there anything I can do? I've been pondering on two
ways to go around this...
1) move the methods from class cell to class black_box
and hence have something like this
black_box::update_one_cell(cell &which);
but I feel that the cell updating method belongs more to the
cell class than to the underlying black_box class, so I have
some doubts.
2) the other way I thought of was to pass a pointer to
the relevant black_box as an extra parameter to each cell
method, like this:
cell::update(black_box *silly_parent_pointer);
So... which would be better (I don't like either), or is there any
other, more natural, workaround?
TIA,
- J.