Thread: Vector of pointers to vector of objects

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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