Thread: Pointers

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    Pointers

    I have a bit of a design problem. I need to have an array of objects, however this array gets resized and elements moved. I need a way to keep a reference to each element without knowing where it is and retrn said reference.

    I was thinking of creating something like a templated class with an array of smart pointers and have a lookup table for the pointers and use that table to return the reference to the desired element or something along those lines...

    Any ideas?

  2. #2

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    19
    But how do I keep a reference to an object?
    I am trying to create something to keep all of my objects of a type, or derived type, in one place. New objects get created and deleted all the time. Now a pointer to an object in a vector is fine, until the vector get resized, isn't it possible for the memory block to be moved somewhere different alltogether? This would mean that my initial pointer to the object would now be useless! I need to be able to have a pointer, or a UID I can use to keep track of these elements no matter where they are in the list or memory location!

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    std::vector<BaseClass*> myBase;
    
    myBase.push_back(new ChildClass);
    myBase.push_back(new ChildChildClass);
    
    myBase.front()->DisplayMsg();
    myBase[1]->DisplayMsg();
    
    delete myBase[0];
    delete myBase.begin() + 1;
    
    myBase.remove(myBase.begin(), myBase.end());
    This code is pointless but it's just to show you that you don't need to have your pointers outside of the vector, you can just pick them as you want.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    19
    I see where you coming from, fair enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM