|
of what an Array Class might look like. The main reason I know of is that arrays and inheritance don't mix well at all. If class Derived inherits from class _base_, you can pass a pointer or reference to a Derived _object_ to any function that requires a _base_ _object_. You can also pass an array ofDerived _object_s to a function that requires an array of _base_ _object_s, i.e. void foo(_base_ my_base_[]); Derived myderived[10]); foo(myderived); This is INCREDIBLY dangerous. If Derived has any additional data members that _base_ doesn't have, then you will get some very mysterious errors as you advance through the array. Each time you increment your index variable you will move forward sizeof(_base_) bytes, while each _object_ actually in the array will actually be of size sizeof(Derived). This will leave both you and the program hopelessly confused. See Scott Meyer's Effective C++ for more info. As for an Array class, the Vector and Deque templates from the STL should take care of almost all of your array needs. Use the Vector unless you need to do a lot of inserting into the front of the array; in that case use the Deque. Both of these classes can grow as necessary, so you don't have to know at compile time the largest possible size of the array. (This is another reason why they are considered better that plain arrays.)
|