I used to think that C couldn't be used to create a truly encapsulated data type. I am very pleased to say that I was wrong. One of P. J. Plauger's essays showed me how (see References, below) to do it without incurring any runtime penalty or giving up protoype checking. Although I've written the example using ISO C the same technique can be used with K&R, aka traditional, C (see the Plauger reference for details).
One day I might give a lecture about this technique. In the meantime, I hope the commented example (below) will help someone else to understand how to do this.
This document is composed of three units: an example, some references, and some suggested further reading.
open_files
variable even though it isn't used in the rest of
the example. I think it is instructive.error()
from K&R and a driver for the Stream
ADT.const
function pointers.The links here lead to my comments on these books in my list of recommended technical books.
Bobby Schmidt discussed several ways of encapsulation in the The Learning C/C++urve column of C/C++ User's Journal during 1997. See in particular:
NOTE: In April 2019 a watchful surfer informed me that the website at www.cuj.com is no longer the homepage of the journal named above. (Thanks `A.S.'!)
The method I present is discussed in the June column (because I wrote to Bobby Schmidt about it).
I have a list of various resources for undergraduate computer science students at UWO.
This document is copyright by its author, J. Blustein. Last modification 01 April 2019 (removed live link to former C/C++ Users Journal homepage); Penultimate modification 19 June 1997 (included references to Bobby Schmidt's columns); Previous modification 31 July 1996.