We can also create container classes that simply provide the basic functions for a particular data type (in Java these are called Interfaces)
typedef struct linkedlist_generic{
/* Just showing one example function to save space */
void * (*get)( struct list_generic &this, unsigned int i );
void * list_head;
}
void * get( struct list_generic &this, unsigned int i ) {
void * curr_pos = this->list_head;
int j;
for( j = 0; j < i curr_pos->next != NULL; j++) curr_pos = curr_pos->next;
return curr_pos->next;
}
Obviously, the biggest disadvantage here is that there won't be any compile-time type checking without some serious preprocessor hackage. Of course the easiest way to deal with that is to simply create a different class for each data type instead of using generics. (This also helps save space by not incurring the extra overhead of managing huge numbers of functions)
Notice that these structures cannot be modified after creation! We can change the functions pointed to in the structure, but we cannot add fields dynamically. In order to create dynamic objects, the easiest data structure to use is a simple table structure (such as a simplified hash table or associative array) that can dynamically resized. Once a
typedef struct linkedlist_generic{
/* Just showing one example function to save space */
void * (*get)( struct list_generic &this, unsigned int i );
void * list_head;
}
void * get( struct list_generic &this, unsigned int i ) {
void * curr_pos = this->list_head;
int j;
for( j = 0; j < i curr_pos->next != NULL; j++) curr_pos = curr_pos->next;
return curr_pos->next;
}
Obviously, the biggest disadvantage here is that there won't be any compile-time type checking without some serious preprocessor hackage. Of course the easiest way to deal with that is to simply create a different class for each data type instead of using generics. (This also helps save space by not incurring the extra overhead of managing huge numbers of functions)
Notice that these structures cannot be modified after creation! We can change the functions pointed to in the structure, but we cannot add fields dynamically. In order to create dynamic objects, the easiest data structure to use is a simple table structure (such as a simplified hash table or associative array) that can dynamically resized. Once a