473,379 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

implementing an opaque type

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?
Feb 26 '07 #1
3 6621
On Feb 26, 4:51 pm, Richard Weeks <rwe...@nomail.netwrote:
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

Feb 26 '07 #2
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.

--
Er*********@sun.com
Feb 26 '07 #3
"Richard Weeks" <rw****@nomail.netwrote in message
news:nDIEh.1150202$R63.671722@pd7urf1no...
>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....b16567e49c42fc

Does that help you at all?
Feb 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Capstar | last post by:
Hi NG, I've read some time ago in this NG, that when you're writing a library it can be good practice to use an opaque type to point to your structures if the user doesn't need to modify the...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
18
by: chankl | last post by:
Can anyone explain what's an opaque pointer and how it's implemented in C? I read about this concept in the book "C interfaces and implementations". Here's an example from the book (list.h -...
27
by: Nate Eldredge | last post by:
Consider the following pseudo-code: #include <opaque.h> struct foo { int a; opaque_t op; int b; };
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.