implementing an opaque type

R

Richard Weeks

I have a library of functions for operations on complex numbers
and a complex type defined as a struct in a header also
containing the prototypes for the library. I want to make this an
opaque type, i.e. I don't want the real and imaginary fields to
be visible to the user of the interface. I tried doing this:

static struct cplex {
double real;
double imag;
};

but the compiler complained "storage class not legal in this
context." How can I implement cplex as an opaque type?
 
D

David Resnick

I have a library of functions for operations on complex numbers
and a complex type defined as a struct in a header also
containing the prototypes for the library. I want to make this an
opaque type, i.e. I don't want the real and imaginary fields to
be visible to the user of the interface. I tried doing this:

static struct cplex {
double real;
double imag;

};

but the compiler complained "storage class not legal in this
context." How can I implement cplex as an opaque type?

Have a look at this from the FAQ:

http://c-faq.com/struct/opaquetypes.html

Look at their example too.

-David
 
E

Eric Sosman

Richard Weeks wrote On 02/26/07 16:51,:
I have a library of functions for operations on complex numbers
and a complex type defined as a struct in a header also
containing the prototypes for the library. I want to make this an
opaque type, i.e. I don't want the real and imaginary fields to
be visible to the user of the interface. I tried doing this:

static struct cplex {
double real;
double imag;
};

but the compiler complained "storage class not legal in this
context." How can I implement cplex as an opaque type?

The "clean" way is to have the header describe your
functions as accepting and delivering `struct cplex *'
pointer values, but omit any description of the struct
itself. That description instead goes into a "private"
header that your library code #includes, but that isn't
"exported" for use by the clients. One consequence is
that since your clients don't know what `struct cplex'
looks like, they can't create struct instances without
help: You will need cplex_create() and cplex_destroy()
functions, or something equivalent.

The "better" way is to find a C99 implementation and
use the complex arithmetic features it provides. At the
very least, make your code look as much like that of C99
as you can, so an eventual conversion (perhaps to C0x)
will be relatively painless.
 
C

Chris Thomasson

Richard Weeks said:
I have a library of functions for operations on complex numbers and a
complex type defined as a struct in a header also containing the prototypes
for the library. I want to make this an opaque type, i.e. I don't want the
real and imaginary fields to be visible to the user of the interface. I
tried doing this:

static struct cplex {
double real;
double imag;
};

http://groups.google.com/group/comp.lang.c/msg/dbb16567e49c42fc

Does that help you at all?
 

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,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top