Opaque data type

L

Labora

Hi All,
Could you tell how to implement opaque data type
with an example ? Or link to a documentation will
be great !!

Thanks,
Labora.
 
M

Michael Mair

Labora said:
Hi All,
Could you tell how to implement opaque data type
with an example ? Or link to a documentation will
be great !!

An opaque data type is one you can only operate on
using functions or macros. So, define your data type
somewhere and provide the stuff the users need to work
with it. They do not need to know how you organised the
data.
In fact, that is the whole point: If you reorganise
the data, then the user interface will not change --
you only have to provide other definitions for the
functions and macros.

Example: FILE from <stdio.h>.
You get a FILE * at opening a file with fopen(), can use
it to read or write to file with fputs()/fprintf() or
fgets()/fscanf() and others, respectively, can
store positions with fgetpos() (in another opaque data type)
and set positions with fsetpos() (using the return value
of a previous fgetpos() call) [there also is fseek() but
this should no longer be used nowadays] or rewind(),
can delete files, and you can close the file with fclose()
-- but you never, ever directly access an entry of FILE
(say a struct member if FILE is a structure).


Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top