By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,107 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,107 IT Pros & Developers. It's quick & easy.

Pointer vs Reference for use in array members

P: n/a
Hi,

I've got a program whose main object contains an array of "cells", each
of which contains an array of "sub-cells", each of which contains an array
of "sub-sub-cells". The cells, subcells and subsubcells are three
user-defined types, each of which contains some program logic as well as
some drawing capabilities. Since all cells (and subcells and subsubcells)
will utilize identical drawing "settings", I'm using a single settings
object, owned by the main program object, which is referred to by the
various cell-type objects when drawing (to control colors, etc.).

The issue I'm having stems from the desire to avoid pointers in favor of
references. As it is, the three cell-types each have an Initialize function
which gets passed a pouinter to the main object's settings object. This is
used to set a member pointer to that object, and later dereferenced to get
at the specific settings when it's a given cell-type object's turn to draw
itself.

What I wanted to do was to use a reference variable instead of a pointer
to that main settings object. But this can't be set from Initialize, since
a reference can't be reseated. And, I can't initialize it via the
constructor, since the cells are all in member arrays of their containing
parent, and the objects in a member array are always initialized via the
default constructor.

What's the best way to proceed? Is my current use of a member pointer
the best/only way? Or is there a clean way to use a member reference here?
I don't mind the pointers, really (and can use asserts to help prevent later
errors in coding that might accidently lead to my attempting to dereference
the pointer prior to its getting initialized properly). But I prefer
references to pointers, and wondered if there was a way to use them here,
without it being a big, unmaintainable, task.

Thanks,
-H

Jan 3 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jan 3, 1:10 pm, "Howard" <m...@here.comwrote:
Hi,

I've got a program whose main object contains an array of "cells", each
of which contains an array of "sub-cells", each of which contains an array
of "sub-sub-cells". The cells, subcells and subsubcells are three
user-defined types, each of which contains some program logic as well as
some drawing capabilities. Since all cells (and subcells and subsubcells)
will utilize identical drawing "settings", I'm using a single settings
object, owned by the main program object, which is referred to by the
various cell-type objects when drawing (to control colors, etc.).

The issue I'm having stems from the desire to avoid pointers in favor of
references. As it is, the three cell-types each have an Initialize function
which gets passed a pouinter to the main object's settings object. This is
used to set a member pointer to that object, and later dereferenced to get
at the specific settings when it's a given cell-type object's turn to draw
itself.

What I wanted to do was to use a reference variable instead of a pointer
to that main settings object. But this can't be set from Initialize, since
a reference can't be reseated. And, I can't initialize it via the
constructor, since the cells are all in member arrays of their containing
parent, and the objects in a member array are always initialized via the
default constructor.

What's the best way to proceed? Is my current use of a member pointer
the best/only way? Or is there a clean way to use a member reference here?
I don't mind the pointers, really (and can use asserts to help prevent later
errors in coding that might accidently lead to my attempting to dereference
the pointer prior to its getting initialized properly). But I prefer
references to pointers, and wondered if there was a way to use them here,
without it being a big, unmaintainable, task.
Sounds like your settings should be a Singleton. If so, and you can
ensure it is created before any of your cells, subcells, etc., you can
pass a reference to the settings object to each cell's constructor (and
therefore to each subcell's constructor, etc.). Trying to do it through
a subsequent Initialize() function won't work, as you've found, since
the cell objects are then fully constructed and so the settings
reference needs to have already been set by then.

It is generally recommended to avoid using an explicit Initialize()
function, since that's what a constructor is supposed to do. Granted,
there are situations where there is a good case for that sort of
design, but if you can avoid it in your particular case, it will clear
up your problem of reference versus pointer to the settings object.

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia

Jan 3 '07 #2

P: n/a

"Craig Scott" <au**********@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...

Sounds like your settings should be a Singleton. If so, and you can
ensure it is created before any of your cells, subcells, etc., you can
pass a reference to the settings object to each cell's constructor (and
therefore to each subcell's constructor, etc.). Trying to do it through
a subsequent Initialize() function won't work, as you've found, since
the cell objects are then fully constructed and so the settings
reference needs to have already been set by then.

It is generally recommended to avoid using an explicit Initialize()
function, since that's what a constructor is supposed to do. Granted,
there are situations where there is a good case for that sort of
design, but if you can avoid it in your particular case, it will clear
up your problem of reference versus pointer to the settings object.
The problem is that I _can't_ pass the reference to the constructors,
because the cells are in member arrays of their parent containers. Member
array initialization always uses the default constructor. The only way to
use a parameterized constructor would be to dynamically allocate the arrays,
and there's no reason for me to do that. I dont want to introduce a slew of
pointers just to make this one minor pointer issue go away.

There's also the possibility of using a global variable for the settings,
but I dislike that even more.

Thanks,
-H
Jan 3 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.