473,785 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1731
On Jan 3, 1:10 pm, "Howard" <m...@here.comw rote:
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**********@g mail.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.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
4739
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 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5   -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
67
4488
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 achieve this would be a linear search through the array: int ptrInArrSafe(T *p,T *a,int max) /* check whether p points to an element of a */
204
13121
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 = {0,1,2,4,9};
42
5347
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
2250
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, then allocate new left side array. if ( size != right->size ) {
41
3687
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 pointer and reference are and how they relate to each other.
14
1989
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 convert an object of this type to the following type?
31
1992
by: huili80 | last post by:
Say I have two classes: class A { public: int x; }; class B {
3
1979
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 like two chips. A Object modifies data in the unsigned char array. Then, B Object is in turn to modify data in the unsigned char array. Static data member is not the answer. Unsigned char array in the file scope is a bad idea because other...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.