How to get Ada private types in C

MHx7002 mnc at m10ux.UUCP
Tue Mar 10 03:23:22 AEST 1987


One of the important features of Ada that allows the safe implementation of
abstract data types is the "private" type feature.  You can declare a new
type in the specification of a module, but indicate that the details of the
type definition may not be seen by users of the module.  These users may
only declare variables of that type.  To perform any operations on the
variables, they must be passed as arguments to functions of the module
that "owns" the private type.  Obviously this is necessary to prevent users
of an abstract data type from writing code that depends on the particular
current implementation of the type, say as an array.  Such a dependency
would prevent changing the implementation later.

Wouldn't it be nice if the same feature could be obtained in a C header file
(which is the analog of an Ada specification)?  Well the following program
compiles without errors on Berkeley 4.3 and Sys V.2 (VAX versions):

	typedef struct PRIVATE_S *PRIVATE_P;
	main()
	{ PRIVATE_P x; if (x) exit();
	}

If this is legal C everywhere, it implies that one can obtain a private type
by declaring it to be a struct pointer in the header file (PRIVATE_P), but
only declaring the struct type (PRIVATE_S) in the module that owns the type.
Comments?
-- 
Michael Condict		{ihnp4|vax135|cuae2}!m10ux!mnc
AT&T Bell Labs		(201)582-5911    MH 3B-416
Murray Hill, NJ



More information about the Comp.lang.c mailing list