4.4 How does this magic work?

A magician never reveals how magic works!

However, how the ADT fraction works is no magic. The key to this enforcible ADT is the ability to cast pointers. From the consumer's point of view, there is only the dummy struct type struct Frac. Since struct Frac has no data member, there is no way the consumer can access values of a fraction directly. The prototypes defined in frac.h tells a consumer what operations can be done with a struct Frac object, but it does not indicate how things are done.

From the provider's point of view, the actual implementation of a fraction is needed. This is the definition of struct _Frac. Note that the subroutines defined in frac.c (provider) need to be consistent with the prototypes defined in frac.h.

Each function in frac.c requires at least one parameter of the type struct Frac *. When the function needs to access the actual values of a fraction, the pointer is first casted into struct _Frac * using the inline function _FracCast. The casted pointer can, then, be used to access actual values (data members) of the real definition of a fraction.

Copyright © 2006-09-07 by Tak Auyeung