Lost references?

A

Andrew

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 = 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 = *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->getID() << endl;


return 0;

}
 
G

Gianni Mariani

Andrew said:
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 = *tmp;


This above does a copy.
 
D

David Harmon

// Elements 0 - 4 in Bar::group are set to point at
// Elements 5 - 9 of Bar::group
mainlist = *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) 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?
 
A

Andrew

David Harmon said:
// Elements 0 - 4 in Bar::group are set to point at
// Elements 5 - 9 of Bar::group
mainlist = *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) 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 = this; ? So by
replacing an object in mainlist doesn't that change what Bar::group is
pointing to?
 
D

David Harmon

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 = 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])


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

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,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top