Thread: Vector of pointers to vector of objects

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    42

    Vector of pointers to objects stored in a vector

    So I'm working on a program, and I wanted to do something like this:

    Class A has a vector<object>, where all the objects of that type are stored in. Each time a new object is created, I push it back there.

    Class B has a vector<object*>, so each time a new object is created on class A, I wanted to push back a pointer to that object here.

    Basically something like this:

    Code:
    Class A
    {
       ...
       vector<object> vec;
       vector<B> classes;
    };
    
    Class B
    {
       ...
       vector<object*> vec;
    };
    
    A::vec.push_back (something);
    A::classes[some index].add (&A::vec[A::vec.size()-1]);
    So the object would be on class A vector, and one of the classes B would have a pointer to that object, on a vector of pointers.

    I know I could just not use a vector<object> on class A, and simply use new/delete and add it as a pointer to class B, but in this case I'd like to do it this way, and it's just not working. Each time I push back a new pointer on class B, all the other pointers become invalid.

    Note: The code is just there as an example, it's obviously not actual code.

    Any help appreciated!
    Last edited by Litz; 11-06-2009 at 11:16 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    In that case, when you push_back(something), a copy is made of the object. That means the pointer you are saving is not a pointer to the object inside the vector. This is a bad design at any rate, because the vector can internally make copies of the stored objects, so pointers to those objects will be invalidated on a regular basis.

    You should instead allocation objects with new, and store those pointers (or shared_ptrs instead).
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Quote Originally Posted by bithub View Post
    In that case, when you push_back(something), a copy is made of the object. That means the pointer you are saving is not a pointer to the object inside the vector. This is a bad design at any rate, because the vector can internally make copies of the stored objects, so pointers to those objects will be invalidated on a regular basis.

    You should instead allocation objects with new, and store those pointers (or shared_ptrs instead).
    Oh sorry, my bad, I didn't mean to write A::classes[some index].add (&something), but A::classes[some index].add (&A::vec[A::vec.size()-1]), so I would add the copy the vector created, and not the original value. That's why I don't get why it doesn't work. I know the "something" will be destroyed, but shouldn't it work as I'm adding what's inside the vector?

    Editing that part of code on first post by the way.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you really want to do this, even though normally is a bad design, you can do this

    Code:
    A::vec.push_back (something);
    A::classes[some index].vec.add (&vec[vec.size()-1]);
    EDIT: Oh, you need the vec, since you have a vector of B obects (classes) and each object has a vector of pointers (vec).
    Last edited by C_ntua; 11-06-2009 at 10:44 AM.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Why are you using pointers to the inner vectors? Will you be storing the same pointer in more than one location (aliasing the vectors)? If not, I don't see the point (pun intended).
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Quote Originally Posted by C_ntua View Post
    If you really want to do this, even though normally is a bad design, you can do this

    Code:
    A::vec.push_back (something);
    A::classes[some index].vec.add (&vec[vec.size()-1]);
    EDIT: Oh, you need the vec, since you have a vector of B obects (classes) and each object has a vector of pointers (vec).
    The add function already does that though. It's something like this:

    Code:
    void B::add(object* x)
    {
       vec.push_back (x);
    }
    So I would pass the adress of the object inside the vector of class A to whatever B::add, and B would push back a pointer to its vector.

    Quote Originally Posted by brewbuck View Post
    Why are you using pointers to the inner vectors? Will you be storing the same pointer in more than one location (aliasing the vectors)? If not, I don't see the point (pun intended).
    Because A has several B's, but object isn't necessarly a member of B. It can be a member of B, or different Bs, or none at all, but it still needs to exist on A.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Litz View Post
    Because A has several B's, but object isn't necessarly a member of B. It can be a member of B, or different Bs, or none at all, but it still needs to exist on A.
    Your post says "vector of pointers to vector of objects." I don't see any pointers to vectors anywhere.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Quote Originally Posted by brewbuck View Post
    Your post says "vector of pointers to vector of objects." I don't see any pointers to vectors anywhere.
    My bad, I'll change it.

    EDIT: Or I would but can't change topic name apparently? It was supposed to say vector of pointers to objects stored in a vector I guess.
    Last edited by Litz; 11-06-2009 at 11:19 AM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Litz View Post
    Oh sorry, my bad, I didn't mean to write A::classes[some index].add (&something), but A::classes[some index].add (&A::vec[A::vec.size()-1]), so I would add the copy the vector created, and not the original value. That's why I don't get why it doesn't work. I know the "something" will be destroyed, but shouldn't it work as I'm adding what's inside the vector?

    Editing that part of code on first post by the way.
    That doesn't work. You keep forgetting that when the vector is internally resized (which happens when you add more items), the objects in the vector get copied. This means that the pointers you saved are worthless since the vector object's addresses have all changed.
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Quote Originally Posted by bithub View Post
    That doesn't work. You keep forgetting that when the vector is internally resized (which happens when you add more items), the objects in the vector get copied. This means that the pointers you saved are worthless since the vector object's addresses have all changed.
    Ah, that's right, totally forgot about that. I'll just create a vector of pointers with all the objects on class A and then and another on class B pointing to same place. It will still do what I want it to and it'll work.

    Thanks for the help everyone.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If the vector of objects isn't re-arranged you could also save indexes instead of memory addresses (aka pointers)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Of Pointers With Objects
    By TheTaoOfBill in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2007, 09:37 PM
  2. Problem with pointers to dynamic objects
    By mike_g in forum C++ Programming
    Replies: 20
    Last Post: 04-25-2007, 01:16 PM
  3. pointers and objects
    By System_159 in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2006, 10:01 AM
  4. Pointers to objects on the heap
    By foot in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2005, 12:20 PM
  5. Objects, or pointers to objects?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2001, 12:57 AM