Categories: General
      Date: Sep 17, 2015
     Title: Article: Abstract data types in C
Untitled document Using opaque pointers, true abstract data types can be implemented in C.

Untitled document

An opaque pointer is a pointer that points to a data structure, usually a struct, whose definition is not visible outside the implementing module.

Lets implement a counter with an initial value and an upper limit.We need:

This is what the header file could look like.

{include file="/h/www/sdu/cmsms/src/c/abstract/parts/counter.h.1"}

Note that there is no definition of _Counter, just a declaration, i.e. we say that it exists, but not what it looks like. This is what guarantees that it is, and will remain, an abstract type.

By local convention, each "member function" is prefixed by a lower-cased class name and two underscores.

Before we start on the implementation, we need a few definitions:

{include file="/h/www/sdu/cmsms/src/c/abstract/parts/counter.c.definitions"}

As Counters definition is hidden, the only way to create and delete instances is by using constructors and destructors:

{include file="/h/www/sdu/cmsms/src/c/abstract/parts/counter.c.constructor"}

The actual work is done by:

{include file="/h/www/sdu/cmsms/src/c/abstract/parts/counter.c.meat"}

A short test program:

{include file="/h/www/sdu/cmsms/src/c/abstract/parts/counter.c.main"}

{php} #ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'/h/www/sdu/cmsms/'); #phpinfo(); #print ini_get('include_path'); {/php}