473,388 Members | 1,326 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,388 software developers and data experts.

Pointer vs Reference for use in array members

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
2 1710
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
3
by: josh | last post by:
Why if I use: Array *Array::operator=( const Array *right ) { if ( right != this ) { // check for self-assignment // for arrays of different sizes, deallocate original // left side array,...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
31
by: huili80 | last post by:
Say I have two classes: class A { public: int x; }; class B {
3
by: Immortal_Nephi | last post by:
Sometimes, unsigned char array is located in the file scope. You define A Object and B Object. A Object and B Object need to share unsigned char array. They are very different object. They are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.