I
ImpalerCore
C++ doesn't use inheritance in its container classes. Having seen
inheritance based conatiner classes I tend to think its the wrong
mechanism.
That's absolutely right imo. If you follow C++'s methodology, the
linked list, hash table, auto-resizing array have their own specific
interfaces (I don't remember seeing any public/private inheritance in
them last time I looked). I don't think that there is much that they
can commonly inherit. The best abstraction for containers is
iterators and algorithms built on using those iterators. And while it
will require calling a container specific function to get a container
specific iterator, the iterators hopefully can share the same
interface regardless of what container it was created from.
In C++, you can define a container specific iterator, but use a common
interface between those iterators.
vector::iterator --> would have to become 'struct array_iterator'
list::iterator --> would have to become 'struct list_iterator'
In C, these iterators would need to have a reference to the container
they use, and a table of function pointers to traverse the container,
access the element, and comparisons (to check whether you're at the
beginning or end of a container), maybe more. The function pointers
can share the same name so that access can be abstracted to an array
or list container.
---Random code segment---
my_list_t* list;
/* Fill up the list with stuff */
my_list_iterator_t l = my_list_iterator_create( list,
my_list_front( list ) );
or maybe
my_list_iterator_t l = list->begin();
/* list->end() is a function pointer that creates a iterator pointing
to the end of the list. */
while ( !my_list_iterator_equal( l, list->end() ) )
{
my_list_iterator_assign( l, object );
l = l->next();
}
Without operator overloading, it gets quite clunky to use iterators in
C compared to C++ containers, perhaps with a bad enough taste that
people will decide to just use C++. This is where C++ really has C
beat in the container arena imo.
At the moment I have no idea how to approach it, as it just seems too
messy to be worth the effort; plus integrating the generic part into a
list or array C interface has to come first. I believe that generic
container specific interfaces do have value in C, but I'm not
convinced about generalizing the interface to them via iterators since
I'm too spoiled from my C++ experience with them. If I saw a C
interface that made sense, sure, but I don't have one that I like.
Just some more thoughts to toss in the salad bowl.