Skip to main content
GameDev.net gamedev.net
🔒 Locked

Template Arrays

Started by Metorical Dec 18, 2002 at 12:02 PM 3 replies 300+ views
Original Post
Metorical
Metorical
Hi, Does anyone know a good place to find information on making a template array class. I''ve looked them up before and I can''t find anything that starts at the beginning (or basics). I have heard that the STL has a vector class which is basically this but this isn''t what I want as a) It is quite complicated b) I want to program it myself. I have a rough idea what to do for most of it but I really need help with the memory allocation as this is probably my weakest point. Specification: Array of any ABT. Dynamic Resizing. Array Bounds functions. Clear and Fast Code. I have made and used template linked lists but these tend to get slow when you have a large number of elements. Thanks in advance, Meto
Zipster
Zipster
vector isn''t really that complicated, if at all. You got a nice interface to use, it handles all the memory and stuff, and it even overloads the [] operator (!). Now, if that''s not the best reason to use it, then heck, what is?

A templated array class is actually quite simple. Assuming we have type T, each array element is going to be sizeof<T> bytes. To access the Nth element of the array, it''s simply N*sizeof<T> into a block of memory. Now, you have to handle the memory, and make sure it increases as more items are added, but it shouldn''t be too difficult. Keep track of the number of elements in the array, provide access only through member functions or overloaded operators, and you can control the bounds checking, maybe throw an exception if the user doesn''t behave
Void
Void
CUJ experts have an excellent article on template array class in their archives (www.cuj.com).

You can check out the template array class at www.boost.org also.

But if you find std::vector hard, I doubt you will find the implementation of a template array class any easier.
Metorical
Metorical
Hi,

Maybe I haven't given the STL enough of a chance. I think I decided it was complicated based on the fact that it was so generalised which is a big mistake.

Thanks very much for the help!

-Meto

ƒJƒCƒ“

[edited by - Metorical on December 19, 2002 12:38:50 PM]
daerid
daerid
quote:
Original post by Metorical
Maybe I haven''t given the STL enough of a chance. I think I decided it was complicated based on the fact that it was so generalised which is a big mistake.



I really, really, really hope you don''t honestly believe that.
The STL is an incredibly powerful tool. It being generalized is the core of its strength.
daerid@gmail.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.