473,394 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Simple class design issue, pointer to parent needed

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.
Jul 19 '05 #1
2 4726

"Jacek Dziedzic" <ja***********@janowo.net> wrote in message
news:31**************@janowo.net...
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.).


How about passing a pointer to the black_box_details_struct in your
update_state call? That's the info you said you need, so it's what you
should pass.

-Howard

Jul 19 '05 #2
"Jacek Dziedzic" <ja***********@janowo.net> wrote in message
news:31**************@janowo.net...
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.).

I would go for Howard's suggestion, if the 'black_box' class is the only
one doing the calls. If the client code is calling 'update_state' on a cell
(which I think is possible), then I can imagine this is pretty much just as
'ugly' as your second solution.

You might consider restructuring the design a bit. You move the actual
'internal_state' variables into 'black_box' as a private int array
(replacing the 'cell' array) and let 'black_box' have a member function
called 'get_cell' or similar:

cell_proxy get_cell (size_t idx);

As the name suggest, you only return a proxy object, which will get all
neeeded information upon construction and thru which you can do all
manipulation. The proxy object basically does the same as you current 'cell'
class, except that the actual data is stored in 'black_box'. A decent
compiler should optimize it so you will have no difference in speed.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
6
by: Code4u | last post by:
I need to design data storage classes and operators for an image processing system that must support a range of basic data types of different lengths i.e. float, int, char, double. I have a...
5
by: EqDev | last post by:
I have a class that is a control derived from UserControl. I want to use serialization and deserialization with this calss but I get an exception "Cannot serialize member...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
38
by: JTL | last post by:
I have learnt java before and now begin to learn c++ what puzzle me is that seem that different SDKs(c++builder, vs.net, gcc..) has its own class library we know that in java there are only one...
3
by: krzysztof.konopko | last post by:
Hello! I want to design a class hierarchy with one base abstract class, let's say CBase. I have a predicate that every object in the class hierarchy must have a parent object of type from this...
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
1
by: johnsonlau | last post by:
When a class derives from a class, You can use a pointer to the parent class to delete the instance of child only when a virtual destructor declared in the parent. class Parent { virtual...
9
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.