Cover by Andy Oram, Greg Wilson

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

A Little Polymorphism Can Go a Long Way

Inside most programs of any flexibility, there is likely to be a polymorphic object of some sort. The table that takes up most of the main page of the Gene Sorter is composed of a series of polymorphic column objects.

Making polymorphic objects in C is not as easy as it is in more modern object-oriented languages, but it can be done in a relatively straightforward manner using a struct in place of an object, and function pointers in place of polymorphic methods. Example 13-1 shows a somewhat abbreviated version of the C code for the column object.

Example 13-1. The column structure, a polymorphic object in C

struct column
/* A column in the big table. The central data structure for
 * hgNear. */
   {
   /* Data set guaranteed to be in each column. */
   struct column *next;   /* Next column in list. */
   char *name;            /* Column name, not seen by user. */
   char *shortLabel;      /* Column label. */
   char *longLabel;       /* Column description. */

   /* -- Methods -- */
   void (*cellPrint)(struct column *col, struct genePos *gp,
        struct sqlConnection *conn);
   /* Print one cell of this column in HTML. */

   void (*labelPrint)(struct column *col);
   /* Print the label in the label row. */
   void (*filterControls)(struct column *col,
        struct sqlConnection *conn);
   /* Printout controls for advanced filter. */

   struct genePos *(*advFilter)(struct column *col, struct sqlConnection *conn, /* Return list of positions for advanced filter. */ /* Lookup tables use the next few fields. */ char *table; ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required