Lost references?

Discussion in 'C++' started by Andrew, Feb 9, 2004.

  1. Andrew

    Andrew Guest

    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;

    }
     
    Andrew, Feb 9, 2004
    #1
    1. Advertising

  2. 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 = *tmp;


    This above does a copy.
     
    Gianni Mariani, Feb 9, 2004
    #2
    1. Advertising

  3. Andrew

    David Harmon Guest

    On 9 Feb 2004 08:45:25 -0800 in comp.lang.c++,
    (Andrew) was alleged to have written:
    > // 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?
     
    David Harmon, Feb 9, 2004
    #3
  4. Andrew

    Andrew Guest

    David Harmon <> wrote in message news:<>...
    > On 9 Feb 2004 08:45:25 -0800 in comp.lang.c++,
    > (Andrew) was alleged to have written:
    > > // 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?

    >
    > 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?
     
    Andrew, Feb 10, 2004
    #4
  5. Andrew

    David Harmon Guest

    On 10 Feb 2004 07:39:25 -0800 in comp.lang.c++,
    (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 = 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 = *tmp;


    Copies values from one object to another. Both before and after the
    copying, they are two distinct objects occupying different storage.
     
    David Harmon, Feb 10, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    459
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    472
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    626
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    735
    Bruno Desthuilliers
    Dec 12, 2006
  5. Replies:
    2
    Views:
    108
Loading...

Share This Page