Pointer vs Reference for use in array members

H

Howard

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
 
C

Craig Scott

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.
 
H

Howard

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top