Resizable array of references?

This is a discussion on Resizable array of references? within the C++ Programming forums, part of the General Programming Boards category; Does C++ have some kind of resizeable array that can contain references? I need to store references and be able ...

  1. #1
    JMK
    JMK is offline
    Registered User
    Join Date
    May 2010
    Posts
    24

    Resizable array of references?

    Does C++ have some kind of resizeable array that can contain references? I need to store references and be able to iterate through them as fast as possible. It may have any number of objects stored in it, and the number of objects stored will change. Is a vector appropriate for this, or should I use something else?
    Last edited by JMK; 05-29-2010 at 08:48 PM.

  2. #2
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    You can use a vector.

    Example:
    Code:
    #include <vector>
    
    using namespace std;
    
    vector <int&> vectorOfIntReferences; //you now have a vector of int references
    Replace "int" with whatever datatype you're making references to.

  3. #3
    JMK
    JMK is offline
    Registered User
    Join Date
    May 2010
    Posts
    24
    How do I declare that as a member of a class and initialize it with 0 length?:
    Code:
    class Entity {
    public:
    std::vector<Entity&> kids;
    };

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    You don't have to initialize it to any size, since its a resizable array.
    All you do is, if you want the vector as a member of a class, then you would want to add a public function which can push_back() elements to the vector, which can be a private member instead of public.

    Something that looks like this:

    Code:
    class Entity {
       public:
         void addElementsToVector(Entity &ref);
       private:
         std::vector<Entity&> kids;
    };
    
    void Entity::addElementsToVector(std::vector<Entity&> &ref) {
    
      kids.push_back(ref); //add the reference to the vector
    
    }
    
    int main() {
    
      Entity object; //create object of class
      Entity object2; //create another object of class
      object.addElementsToVector(object); //add a reference to 'object' to your vector
      object.addElementsToVector(object2); //add a reference to 'object2' to your vector
    
      return 0;
    
    }
    Last edited by Programmer_P; 05-29-2010 at 11:15 PM. Reason: Fixed typo

  5. #5
    JMK
    JMK is offline
    Registered User
    Join Date
    May 2010
    Posts
    24
    Adding this in the class declaration:
    Code:
    std::vector<Entity&> kids;
    Causes this error:
    1>c:\program files\microsoft visual studio 9.0\vc\include\xmemory(95) : error C2528: 'pointer' : pointer to reference is illegal

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Not sure if you can use a vector.

    References have to be initialized and cannot be assigned.

    May have to use a vector of pointers.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, my bad. Guess its not legal then.
    I'm still in the process of learning the language myself, actually.

    Here's a link on where you might want to read up on it:

    vector of references

    It explains why they're not allowed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21