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

Lost references?

Can someone help explain the following?

Why is it that elements 5 - 9 in Bar::group do not seem to resolve to
elements 0 - 4 of Bar::group when de-referenced?

If I am following this correctly I would assume the following is
happening.

Elements 0 - 4 of Bar::group are created when Bar mainlist is
declared in main.

Elements 5 - 9 of Bar::group are created within the for loop
of main.

Within this loop elements 0 - 4 of mainlist which refer to elements
0 - 4 of Bar::group are set to point at elements 5 - 9 of Bar::group.

Once this assignment is done elements 0 - 4 of mainlist and elements
0 - 4 of Bar::group both point to elements 5 - 9 of Bar::group.

But if I make a change to a member of one ot the elements 0 - 4 of
mainlist the change is reflected in elements 0 - 4 of Bar::group but
not in the corresponding 5 - 9 element of Bar::group?

Thanks for any clarification.
#include <iostream.h>

template <class T> class Foo {

public:

T list[10];

};

class Bar {
public:

Bar() {

static int i = 0;

cout << "Creating " << i ;

group.list[i] = this;

id = i;

i++;

cout << " : " << id << endl;

}

void setID(int i) { id = i; }
int getID() { return id; }

Foo<Bar*>* getList() { return &group; }

private:

static Foo<Bar*> group;
int id;

};

Foo<Bar*> Bar::group;

int main() {

int i;

// Elements 0 - 4 are created in Bar::group
Bar mainlist[5];

// Elements 5 - 9 are created in Bar::group
for(i = 0; i < 5; i++) {

Bar *tmp = new Bar();

// Elements 0 - 4 in Bar::group are set to point at
// Elements 5 - 9 of Bar::group
mainlist[i] = *tmp;

}

// Change id of Element 2 of Bar::group
mainlist[0].getList()->list[2]->setID(9999);

// No Change of id in Element 7 in Bar::group ?
for(i = 0; i < 10; i++)
cout << mainlist[0].getList()->list[i]->getID() << endl;
return 0;

}
Jul 22 '05 #1
4 1536
Andrew wrote:
Can someone help explain the following?
....
Within this loop elements 0 - 4 of mainlist which refer to elements
0 - 4 of Bar::group are set to point at elements 5 - 9 of Bar::group.
This statement above is wrong - there are still 10 objects - it's just
that the first 5 are assigned the value of the last 5.

....
// Elements 0 - 4 are created in Bar::group
Bar mainlist[5];

// Elements 5 - 9 are created in Bar::group
for(i = 0; i < 5; i++) {

Bar *tmp = new Bar();

// Elements 0 - 4 in Bar::group are set to point at
// Elements 5 - 9 of Bar::group
mainlist[i] = *tmp;


This above does a copy.

Jul 22 '05 #2
On 9 Feb 2004 08:45:25 -0800 in comp.lang.c++, an***********@ca.com
(Andrew) was alleged to have written:
// Elements 0 - 4 in Bar::group are set to point at
// Elements 5 - 9 of Bar::group
mainlist[i] = *tmp;


Wrong. The above does not touch Bar::group, and it does not change any
pointers. Instead it makes a copy of the object at (*tmp) and replaces
the object at (mainlist[i]) which is an instance of class Bar.

Since elements 0-4 of Bar::group point to elements 0-4 of mainlist, this
means that elements 0-4 point to copies of elements 5-9 in Bar::group.

Thereafter, changing the copies does NOT change the objects they were
formerly copied from. QED.

Please promise not to allocate any objects with "new" nor to juggle any
more pointers unless strictly necessary. It is not good C++ style.
What are you trying to accomplish?

Jul 22 '05 #3
David Harmon <so****@netcom.com> wrote in message news:<40****************@news.west.earthlink.net>. ..
On 9 Feb 2004 08:45:25 -0800 in comp.lang.c++, an***********@ca.com
(Andrew) was alleged to have written:
// Elements 0 - 4 in Bar::group are set to point at
// Elements 5 - 9 of Bar::group
mainlist[i] = *tmp;
Wrong. The above does not touch Bar::group, and it does not change any
pointers. Instead it makes a copy of the object at (*tmp) and replaces
the object at (mainlist[i]) which is an instance of class Bar.


But don't the objects which are in Bar::group point to the objects in
mainlist since the elements in Bar::group are set as references to Bar
in it's constructor with the statement group.list[i] = this; ? So by
replacing an object in mainlist doesn't that change what Bar::group is
pointing to?

Since elements 0-4 of Bar::group point to elements 0-4 of mainlist, this
means that elements 0-4 point to copies of elements 5-9 in Bar::group.

Thereafter, changing the copies does NOT change the objects they were
formerly copied from. QED.

Please promise not to allocate any objects with "new" nor to juggle any
more pointers unless strictly necessary. It is not good C++ style.
What are you trying to accomplish?

Jul 22 '05 #4
On 10 Feb 2004 07:39:25 -0800 in comp.lang.c++, an***********@ca.com
(Andrew) was alleged to have written:

But don't the objects which are in Bar::group point to the objects in
mainlist
Some of them do. The pointers 0-4 in Bar::group point to objects 0-4 in
mainlist.

The pointers 5-9 in Bar::group point to objects later allocated with
"new". (In a real program you would have a problem, as you never
arranged to "delete" those allocated objects.)

mainlist[] is an array of objects. It does not contain pointers or
references, so it can never have objects later allocated with new "in"
it. Instead, it can have distinct copies of them.
since the elements in Bar::group are set as references
pointers
to Bar in it's constructor with the statement group.list[i] = this; ?
Yes. Each entry in Bar::group is set to point to a unique instance of
Bar, and never thereafter changed. So in your example, Bar::group[0]
and Bar::group[5] can never refer to the same object.
So by replacing an object in mainlist doesn't that change what
Bar::group is pointing to?


No. Replacing an object in mainlist does not change any pointer value
in Bar::group. Bar::group still points at the same old objects.
Nothing in your example changes which any Bar::group entry points to
after the initial allocation of some object sets it.

Changing the value of an entry in mainlist does of course change the
value whether it is referenced through *(Bar::group[0]) or mainlist[0].
But it will never be the same object as *(Bar::group[5])
> mainlist[i] = *tmp;


Copies values from one object to another. Both before and after the
copying, they are two distinct objects occupying different storage.

Jul 22 '05 #5

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

Similar topics

24
by: dotnetforfood | last post by:
Joel Spolsky's new article "How Microsoft Lost the API War" at http://www.joelonsoftware.com/articles/APIWar.html describes how .NET has failed, how classic VB6 and ASP continue to be preferred by...
2
by: Kathie | last post by:
I have an application that I distribute as an MDE file that clients run within Access. One of my clients has Win2000 and Office 2000 and recently installed Office SP3. Now they are getting VBA...
2
by: Colleyville Alan | last post by:
I am using Access and have embedded the ActiveX control Formula One that came with Office 2000. (ver 3.04). I have created and formatted a spreadsheet and now I want to copy the info with...
1
by: Eric J Owens | last post by:
Hello and thanks! I have an A2k mdb that suddenly lost its reference to Microsoft Office 9.0 Object Library, yet the backup and a developement copy have not lost the same reference (all copies...
6
by: Patrick De Ridder | last post by:
How do I connect to an event being thrown when a field has lost focus? Could you please give a code example? -- Patrick De Ridder ngmail@freeler.nl
3
by: AC | last post by:
This happened to me once before and luckily I have been backing up regularly - but as i'm designing and testing, I went back to a page (heavily designed with many particular spacings, control...
7
by: Jimmie H. Apsey | last post by:
Referential Integrity on one of our production tables seems to have been lost. I am running Postgres 7.1.3 embedded within Red Hat kernel-2.4.9-e.49. Within that I have a table with referential...
3
by: Daniel Manes | last post by:
I need a strategy to debug this situation... I can't put all the code involved, but here are some of the critical lines with comments: ------------------------- Private _parentDataCell As...
1
by: pmasclark | last post by:
Hello, I created a web site, site A, that redirects to another web site, site B, where a simple web service is hosted. The code to call the web service is simple. oWS.AllowAutoRedirect = True...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...

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.