Custom List Class

Dec 22, 2008 at 7:24pm
Hey everyone,

I'm trying to write a custom List class that stores and manages references to other objects. What I would like to know is, whats the best practice for storing the data? Should I store the address' of the objects, or the objects themselves? The reason I went with storing the address was because I will need to store references to the same objects in more than one of these lists. I do not want to store copies of the same object.

Right now, I'm allocating the data like so:

T data** = T*[size]; //<typename T>

To store the objects I have a function:

1
2
3
4
5
6
template <typename T>
List<T>::Add(T &item) {

    this->data[count] = &item;

}


Does this make sense? Or should I use just an array of pointers like so:

T data* = T[size];

Thanks in advance!
Dec 22, 2008 at 8:42pm
You can still make links use the data in links in a different list by making the second list of type T *.

In reality, it doesn't really matter how you keep the data.
I think it's best to store the objects themselves, though. I would make sense to store a pointer to the object if you (you, as in the designed of the list, not as in the user of the list) were expecting to transfer control to it between links, or having two links point to the same object, but that won't happen.

By the way, that's not a list. That's a vector.
Topic archived. No new replies allowed.